我正在编写一个 Qt 应用程序。
我已将我的应用程序分为 QML 前端和 C++ 插件后端。
在我的 C++ 插件中,我有一个 Session 抽象类,我想将其公开给 QML,我也有该类的一些实现。
我希望我的 QML 前端只知道 Session 类,而不用担心它是哪种 session 的具体细节。
我尝试了 qmlRegister* 的几种变体以使用 QML 注册我的 session 类型,但要么 session 需要具体(如 qmlRegisterType 的情况),要么它注册得很好,但我根本无法像 property Session 中那样从 QML 引用 session 类型session
甚至没有从 QML 实例化 Session。
有谁知道我应该如何处理这个问题?
更新:
无效示例:
在 main.cpp 中:
char const* const uri = "com.nogzatalz.Downow";
qmlRegisterUncreatableType<downow::Session>(uri, 1, 0, "Session", "Abstract type");
在 DowNow.qml 中:
import QtQuick 2.0
import com.nogzatalz.Downow 1.0
Item {
property Session session
}
最佳答案
我用 qmlRegisterInterface 得到了这个. 我的虚拟调用是 InputDeviceConfigurator:
class InputDeviceConfigurator : public QObject
{
Q_OBJECT
public:
explicit InputDeviceConfigurator(QObject *parent = 0);
Q_INVOKABLE virtual QString deviceId() = 0;
}
我注册如下:
qmlRegisterInterface<InputDeviceConfigurator>( "InputDeviceConfigurator" );
然后使用一个继承类JoystickConfigurator:
class JoystickConfigurator : public InputDeviceConfigurator
{
public:
JoystickConfigurator( JoystickDevice * device );
// InputDeviceConfigurator interface
virtual QString deviceId() override;
}
而且我可以使用它:
Component.onCompleted: console.log( UserInputManagement.getConfigurator().deviceId() )
UserInputManagement 只是一个单例:
Q_INVOKABLE InputDeviceConfigurator * getConfigurator();
关于c++ - 在 QML 插件中注册一个 C++ 抽象类并从 QML 中引用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24722502/