gpt4 book ai didi

c++ - 无法从 ‘QAction*’ 转换为 ‘QAction’

转载 作者:行者123 更新时间:2023-11-30 03:04:53 25 4
gpt4 key购买 nike

当我尝试返回值时,我在 smtdplugin 实现文件中遇到了这个错误。好的,我知道我正在创建一个指向 QAction 的指针,当我尝试返回它时,我不能这样做,因为我的方法正在等待对对象的引用。但我不知道该怎么做(我是初学者)如何避免这个问题? , 并成功返回该对象

#include "smtdplugin.h"

QAction SmtdPlugin::newItem() {

QAction *item = new QAction(NULL);

return item; // here i get error
}

Q_EXPORT_PLUGIN2(smtdplugin,SmtdPlugin);

头文件:

#ifndef SMTDPLUGIN_H
#define SMTDPLUGIN_H

#include <QObject>
#include <QAction>
#include "smtdinterface.h"

class SmtdPlugin : public QObject,SmtdInterface {

Q_OBJECT
Q_INTERFACES (SmtdInterface)

public :
QAction newItem();

};

#endif // SMTDPLUGIN_H

接口(interface)类:

#ifndef SMTDINTERFACE_H
#define SMTDONINTERFACE_H

#include <QAction>

class SmtdInterface {

public:
virtual ~SmtdInterface() {}
SmtdInterface();
virtual QAction newItem () = 0;

};

Q_DECLARE_INTERFACE(SmtdInterface,"com.trololo.Plugin.SmtdInterface/1.0")

#endif

最佳答案

我不熟悉 qt 但错误是 -

QAction* SmtdPlugin::newItem()   // Return type should be QAction* and not QAction
{

QAction *item = new QAction(NULL);

return item;
}

item 的类型是 QAction* 而不是编译器提示的 QAction。我认为您对空间感到困惑。

QAction* item ; // QAction * item ; QAction *item ;

以上三个约定意思相同。

关于c++ - 无法从 ‘QAction*’ 转换为 ‘QAction’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8243823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com