c++ - 在 QML 插件中注册一个 C++ 抽象类并从 QML 中引用它

标签 c++ qt qml abstract-class


我正在编写一个 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/

相关文章:

c++ - OpenCL enqueueWriteImage 在 C++ 包装器中但在 C 函数中没有 const void* ptr

java - JNI 将 Java double 转换为 jdouble 时遇到问题

c++ - 如何更新样式表的单个属性?

c++ - 共享库中的 QQuickWindow 在 QApplication 中显示时自动关闭

c++ - OpenCV,DFT 函数不要在带有 IMREAD_COLOR 的图像中使用

c++ - 字符串没有在 C++ 中打印完整的行?

c++ - 将YUV420p QVideoFrame转换为灰度OpenCV垫

c++ - query.evaluteTo 适用于 QStringList 但不适用于 xml 中的特定项目

c++ - QT半透明窗口和远程桌面

c++ - 绑定(bind)的 c++ 值未在 QML 中更新