c++ - 可以在 C++ QObject::connect() 中连接一个 QML 对象现有信号吗?

标签 c++ qt qml signals-slots

QML TreeView 有一个名为:doubleClicked(QModelIndex) 的信号

引用:https://doc.qt.io/qt-5.10/qml-qtquick-controls-treeview.html#doubleClicked-signal

可以在 C++ QObject::connect() 中连接现有信号吗??

我试过这个:

QQmlApplicationEngine engine;
QObject *myTreeMenu = engine.rootObjects().at(0)->findChild<QObject*>("myTreeMenu");
connect(myTreeMenu , SIGNAL(doubleClicked(QModelIndex)), this, SLOT(slotModelClicked(QModelIndex)));

但是我收到这个返回错误:

QObject::connect: No such signal TreeView_QMLTYPE_63_QML_68::doubleClicked(QModelIndex) in '...'
QObject::connect:  (sender name:   'myTreeMenu ')

最佳答案

documentation for Qt 5.11解释了为什么这不是使用 C++ 和 QML 的最佳方式。我建议您改为在 C++ 对象中公开插槽或 Q_INVOKABLE 并在 onDoubleClicked 处理程序中调用它:

Q_INVOKABLE void doStuff();

在 QML 中:

onDoubleClicked: cppObject.doStuff()

文档有一个“之前和之后”的例子来证明这一点;这是它的大部分内容:

With this approach, references to objects are "pulled" from QML. The problem with this is that the C++ logic layer depends on the QML presentation layer. If we were to refactor the QML in such a way that the objectName changes, or some other change breaks the ability for the C++ to find the QML object, our workflow becomes much more complicated and tedious.

Refactoring QML is a lot easier than refactoring C++, so in order to make maintenance pain-free, we should strive to keep C++ types unaware of QML as much as possible. This can be achieved by "pushing" references to C++ types into QML:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    Backend backend;

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("backend", &backend);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

The QML then calls the C++ slot directly:

import QtQuick 2.11
import QtQuick.Controls 2.4

Page {
    Button {
        text: qsTr("Restore default settings")
        onClicked: backend.restoreDefaults()
    }
}

With this approach, the C++ remains unchanged in the event that the QML needs to be refactored in the future.

In the example above, we set a context property on the root context to expose the C++ object to QML. This means that the property is available to every component loaded by the engine. Context properties are useful for objects that must be available as soon as the QML is loaded and cannot be instantiated in QML.

Integrating QML and C++ demonstrates an alternative approach where QML is made aware of a C++ type so that it can instantiate it itself.

关于c++ - 可以在 C++ QObject::connect() 中连接一个 QML 对象现有信号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50219487/

相关文章:

Qt/C - QWebView - 加载 Flash Lite 插件

javascript - QML - 将 JS 函数的代码作为字符串获取

c++ - 顶点和索引缓冲区如何在 DirectX11 中与顶点、法线和 Texcoords 一起工作

c++ - 可以从播放中更改 AlternateViewPointCap 或 MirrorCap

c++ - "Error: no matching function or call"到我的 Windows 析构函数

qt - QML 窗口调整大小/移动闪烁

c++ - 使用MinGW单独编译

c++ - 帮助理解 C++、模板、operator() 的类示例代码

qt - 如何在 Pane 中使用 Material.elevation 和 Radius?

c++ - 当我使用 Maemo5 特定的 Qt 类时出现奇怪的编译错误..!