gpt4 book ai didi

c++ - 在派生类中调用 Q_INVOKABLE 方法

转载 作者:行者123 更新时间:2023-11-28 04:14:45 25 4
gpt4 key购买 nike

<分区>

我有一个有趣的问题。我不知道如何组合从 QObject 继承的两个类。在基类的派生类中使用 Q_INVOKABLE 方法解决我的问题。我想在各种“模型”中多次使用“loadFromJson”。

我已经尝试定义 Q_OBJECT 宏,并重建类的逻辑。

//model.h
class Model : public QObject{
Q_OBJECT
public:
Q_INVOKABLE bool loadFromJson(const QString &data);
}

//sqltablemodel.h
class SqlTableModel : public QSqlTableModel{
//some code
}

//server.h
class Server : public SqlTableModel, Model
{
Q_OBJECT
public:
explicit Server(QString tableName = "");
};

//main.cpp
Server *server = new Server();
context->setContextProperty("server", server);
//

//server.qml
server.loadFromJson("123"); // not working


/*
If i remove QObject from Model then i can't call loadFromJson even Q_OBJECT defined, otherwise Server is ambigous using QObject. But i want to use methods from both base classes SqlTableModel & Model in derived Server with Q_INVOKABLE ability.
*/

main.cpp:41: ошибка: 'QObject' 是 'Server' 的模糊基础 context->setContextProperty("服务器", 服务器);

model.h:27: ошибка: undefined reference to `vtable for Model'

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