久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 信號到 Qt 中的 QML 插槽

C++ signal to QML slot in Qt(C++ 信號到 Qt 中的 QML 插槽)
本文介紹了C++ 信號到 Qt 中的 QML 插槽的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想將信號從 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 欧美片网站免费 | 91精品久久久久久久久久 | 国产精品国产a级 | 狠狠av | 欧美日韩国产免费 | 一区二区三区中文字幕 | 激情福利视频 | 精品一区二区三区四区视频 | 久久久久久免费精品一区二区三区 | 成人亚洲精品久久久久软件 | 国产精品久久久久久久久污网站 | 亚洲一区中文字幕 | 亚洲精品1区 | 久久精品一区二区三区四区 | 精品视频导航 | 国内精品视频在线观看 | 天天操综合网 | 99热这里都是精品 | 91精品在线观看入口 | 国产日韩欧美综合 | 少妇一级淫片免费放播放 | 日韩欧美专区 | 免费黄色av网站 | 国产精品中文在线 | 在线国产一区 | 日韩午夜网站 | 一区日韩 | 奇米影视在线 | 日本黄色影片在线观看 | 日本a级大片 | 亚洲第一在线 | 日本高清视频在线播放 | 亚洲精品成人在线 | 亚洲综合在线播放 | 欧美成人一级 | 一区二区免费在线 | 天天干天天玩天天操 | 四虎av电影 | 国产精品毛片一区二区三区 | 久在线精品视频 | 国产成人综合一区二区三区 |