問題描述
我想將信號從 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 插槽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!