如何在C++代码中连接QML代码中的信号
2015-09-11 14:01
543 查看
在QML应用设计中,C++在很多的时候作为一个语言来为应用做一些需要计算或拓展QML功能的选项.在今天的例程中,我们来介绍如何连接QML代码中的信号.具体更多的阅读,可以参阅文章"Interacting with QML Objects from C++".
为了说明问题的方便,我们还是来贴上我们的代码:
在这里,我们定义了两个slots.第一个slot是用来接受一个QString来作为参数的.第二个slot是用一个通用的QVariant来作为参数,也即它可以用任何一个类型的数据来传人,包括第一种情况.具体关于QML的基本数据类型,可以参阅文章"QML Basic Types".
在上面的代码中,我们把view的rootObject,也即我们下面即将讲到的MainView,连到我们定义的两个signal:
我们的Main.qml设计也非常简单:
运行我们的应用:
应用的输出:
整个项目的源码在:https://github.com/liu-xiao-guo/connectiontoqmlsignal
为了说明问题的方便,我们还是来贴上我们的代码:
myclass.h
#ifndef MYCLASS_H #define MYCLASS_H #include <QObject> #include <QQuickItem> #include <QDebug> class MyClass : public QObject { Q_OBJECT public: MyClass(QObject *parent = 0); public slots: // This is a slot for QString only void cppSlot(const QString &msg) { qDebug() << "Called the C++ slot with message:" << msg; } // This is the slot for a generic data type void cppSlotForGenericData(const QVariant &v) { qDebug() << "Called the C++ slot with value:" << v; QQuickItem *item = qobject_cast<QQuickItem*>(v.value<QObject*>()); qDebug() << "Item dimensions:" << item->width() << item->height(); } }; #endif // MYCLASS_H
在这里,我们定义了两个slots.第一个slot是用来接受一个QString来作为参数的.第二个slot是用一个通用的QVariant来作为参数,也即它可以用任何一个类型的数据来传人,包括第一种情况.具体关于QML的基本数据类型,可以参阅文章"QML Basic Types".
main.cpp
#include <QGuiApplication> #include <QQmlApplicationEngine> #include <QQuickView> #include <QQuickItem> #include "myclass.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQuickView view; view.setSource(QUrl(QStringLiteral("qrc:///Main.qml"))); view.setResizeMode(QQuickView::SizeRootObjectToView); QObject *item = view.rootObject(); MyClass myClass; // Connect a QString data type slot QObject::connect(item, SIGNAL(qmlSignal(QString)), &myClass, SLOT(cppSlot(QString))); // Connect a generic slot QObject::connect(item, SIGNAL(qmlSignalGeneric(QVariant)), &myClass, SLOT(cppSlotForGenericData(QVariant))); view.show(); return app.exec(); }
在上面的代码中,我们把view的rootObject,也即我们下面即将讲到的MainView,连到我们定义的两个signal:
signal qmlSignal(string msg) signal qmlSignalGeneric(var anObject)
我们的Main.qml设计也非常简单:
Main.qml
import QtQuick 2.0 import Ubuntu.Components 1.1 /*! \brief MainView with a Label and Button elements. */ MainView { id: main // objectName for functional testing purposes (autopilot-qt5) objectName: "mainView" // Note! applicationName needs to match the "name" field of the click manifest applicationName: "connectiontoqmlsignal.liu-xiao-guo" /* This property enables the application to change orientation when the device is rotated. The default is false. */ //automaticOrientation: true // Removes the old toolbar and enables new features of the new header. useDeprecatedToolbar: false signal qmlSignal(string msg) signal qmlSignalGeneric(var anObject) width: units.gu(60) height: units.gu(85) Page { id: page title: i18n.tr("connectiontoqmlsignal") Column { anchors.fill: parent spacing: units.gu(2) Button { width: parent.width text: i18n.tr("Send signal to C++!") onClicked: { main.qmlSignal("Hello from QML!") } } Button { width: parent.width text: "Send a generic data to C++" onClicked: { main.qmlSignalGeneric(main) } } } } }
运行我们的应用:
应用的输出:
QML debugging is enabled. Only use this in a safe environment. Loading module: 'libubuntu_application_api_touch_mirclient.so.3.0.0' Called the C++ slot with message: "Hello from QML!" Called the C++ slot with value: QVariant(QObject*, Main_QMLTYPE_46(0xdebec0, name = "mainView") ) Item dimensions: 540 919
整个项目的源码在:https://github.com/liu-xiao-guo/connectiontoqmlsignal
相关文章推荐
- c++ list, vector, map, set 区别与用法比较
- [转]关于C与C++中互相调用动态链接库的问题_卓越李子_新浪博客
- C++ 资源表
- 第二周项目1——c/c++语言中函数参数传递的三种方式
- Item 25:考虑实现一个不抛异常的swap Effective C++笔记
- C语言中auto,register,static,const,volatile的区别详细解析
- C++类模板的使用-排序
- C++ Primer的课后规划问题的第八章
- C语言程序变异的内存分配
- C++基础学习
- C++学习笔记1(带参数的main( )函数)
- Effective C++ 条款42 了解typename的双重意义
- C++数组名的思考
- 【C++拾遗】 explicit关键字
- C++/CLI中MFC与.NET的互操作
- 大数求余
- C++友元函数和友元类
- C++容器使用经验总结
- 第二周上机实践—项目1—C/C++语言中函数参数传递的三种方式
- c语言-位运算