问题描述
我想将信号从 C++ 发送到我的 QML 文件中的插槽.我已经让它在没有原始类型参数的情况下工作,但如果我想将 QString
发送到我的 QML 插槽,我在连接时会收到错误.
I want to send a Signal from C++ to a Slot in my QML File.
I already got it working without and primitive type parameters, although if I want to send a QString
to my QML Slot I get an error whilst connecting.
我在 main.cpp 中连接
I connect in main.cpp
QObject *contentView = rootObject->findChild<QObject*>(QString("contentView"));
QObject::connect(&myObj, SIGNAL(finishedGatheringDataForItem(QString)),
contentView, SLOT(updateViewWithItem(QString)));
我的qml文件的相关部分
the relavant part of my qml File
Rectangle {
objectName: "contentView"
function updateViewWithItem(string) { console.log('got some Items'); } // slot
}
错误:
Object::connect: No such slot QDeclarativeRectangle_QML_2::updateViewWithItem(QString)
推荐答案
我认为最好查看本教程:
I think it would be best if you check this tutorial:
http://doc.qt.io/qt-4.8/qtbinding.html
尤其是这部分:
http://doc.qt.io/qt-4.8/qtbinding.html#receiving-signals
我认为您在这种情况下的错误可能是您没有将其声明为插槽,或者您没有使其可调用.Qt 教程中解释了这两个选项.
I think your mistake in this case might either be that you didn't declare it as a slot or you didn't make it invocable. Both options are explained in the Qt Tutorial.
此外,您需要使用 QVariant 来在 C++ 和 QML 之间交换数据.您还可以注册类型,例如小部件和东西,以便您可以在 QML 中将它们用作本机"类型,例如矩形.在大多数情况下,不建议这样做,除非您需要某些特定的外部类或某些无法在 QML 界面中显示的数据.
Also, you need to use a QVariant in order to exchange data between C++ and QML. You can also register types, e.g. Widgets and stuff, so that you can use them in QML as a "native" type like a rectangle. In most cases this is not recommended, except if you need some certain extern class or some data that you cannot display otherwise in your QML Interface.
QVariant 的原因是 QML 的基于脚本的方法.QVariant 基本上包含您的数据和数据类型的描述,以便 QML 知道如何正确处理它.这就是为什么你必须在 QML 中用 String、int 等指定参数.但与 C++ 的原始数据交换仍然是 QVariant
The reason for the QVariant is the Script based approach of QML. The QVariant basically contains your data and a desription of the data type, so that the QML knows how to handle it properly. That's why you have to specify the parameter in QML with String, int etc.. But the original data exchange with C++ remains a QVariant
我之前用过qmlRegisterType,但是对于简单的数据类型是非常不方便的解决方案.它更适合用于更复杂的数据,例如 QML 本身不支持或扩展 QStandardItemModels
的自定义小部件、画布或视频元素.这是在 QML 和 C++ 之间交换数据的一种更方便的方式,并且首先不需要信号或插槽,因为 QStandardItemModel 会自动更新 GUI.要使用 QStandardItemModel,您需要使用 qmlRegisterType.. 注册类型.然后可以在基于模型的视图中使用模型,例如 ListView 等.
I have used the qmlRegisterType before, but it is a very inconvenient Solution for simple data types. It is rather used for more complex data, such as custom Widgets, Canvas or Video elements that QML does not natively support or extended QStandardItemModels
. It is a more convenient way to exchange data between QML and C++ and does not need Signals or Slots in first instance, because the QStandardItemModel updates the GUI automatically. For using the QStandardItemModel you need to register the Type with qmlRegisterType.. . The Model can then be used in Model based Views such as the ListView etc.
我附上了这个主题的教程,它描述了如何使用 QListModel.
I attached a tutorial for this topic, it describes how to use the QListModel.
http://doc.qt.io/qt-4.8/qdeclarativemodels.html
这篇关于C++ 信号到 Qt 中的 QML 插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!