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

It is possible connect a QML Object existing signal in C++ QObject::connect()?

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)));

但我收到此 return 错误:

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()

文档中有一个 "before and after" 示例对此进行了演示;这是大部分内容:

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.