C ++ SIGNAL到Qt中的QML SLOT [英] C++ SIGNAL to QML SLOT in Qt

查看:358
本文介绍了C ++ SIGNAL到Qt中的QML SLOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C ++发送信号到我的QML文件中的插槽。
我已经得到它工作没有和原始类型参数,虽然如果我想发送一个 QString 到我的QML槽我在连接时得到一个错误。 >

我在main.cpp中连接

  QObject * contentView = rootObject-> findChild< QObject *>(QString(contentView)); 
QObject :: connect(& myObj,SIGNAL(finishedGatheringDataForItem(QString)),
contentView,SLOT(updateViewWithItem(QString)));

我的qml文件的相关部分

  Rectangle {
objectName:contentView
function updateViewWithItem(string){console.log('got some Items'); } // slot
}

错误:

  Object :: connect:No such slot QDeclarativeRectangle_QML_2 :: updateViewWithItem(QString)


解决方案

我认为最好是检查本教程:



http://doc.qt.io/qt-4.8/qtbinding.html



特别是此部分:



http://doc.qt.io/qt-4.8/qtbinding.html#receiving-signals



我认为您在这种情况下的错误可能或者是你没有声明它作为一个插槽,或者你没有使它可调用。这两个选项在Qt教程中解释。



此外,您需要使用QVariant为了在C ++和QML之间交换数据。
您还可以注册类型,例如。小部件和东西,所以你可以使用它们在QML作为一个原生类型像一个矩形。在大多数情况下,不建议这样做,除非你需要一些特定的extern类或一些你不能在QML界面中显示的数据。



QVariant的原因是基于脚本的QML方法。 QVariant基本上包含你的数据和数据类型的描述,所以QML知道如何正确处理它。这就是为什么你必须使用String,int等指定QML中的参数。但是与C ++的原始数据交换仍然是一个QVariant



我之前使用过qmlRegisterType,这是一个非常不方便的解决方案,简单的数据类型。它更适用于更复杂的数据,例如自定义Widgets,Canvas或Video元素,QML本身不支持或扩展 QStandardItemModels 。这是一种在QML和C ++之间交换数据的更方便的方法,并且在第一实例中不需要信号或插槽,因为QStandardItemModel自动更新GUI。要使用QStandardItemModel,您需要使用qmlRegisterType注册类型。然后可以在基于模型的视图中使用该模型,例如ListView等。



我为此主题附加了一个教程,它描述了如何使用QListModel。



http://doc.qt.io/qt-4.8/ qdeclarativemodels.html


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.

I connect in main.cpp

QObject *contentView = rootObject->findChild<QObject*>(QString("contentView"));
QObject::connect(&myObj,      SIGNAL(finishedGatheringDataForItem(QString)), 
                 contentView, SLOT(updateViewWithItem(QString)));

the relavant part of my qml File

Rectangle {
        objectName: "contentView"
        function updateViewWithItem(string) { console.log('got some Items'); }  // slot
}

Error:

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

especially this section:

http://doc.qt.io/qt-4.8/qtbinding.html#receiving-signals

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.

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.

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

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.

I attached a tutorial for this topic, it describes how to use the QListModel.

http://doc.qt.io/qt-4.8/qdeclarativemodels.html

这篇关于C ++ SIGNAL到Qt中的QML SLOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆