如何在C++代码中连接QML代码中的信号
来源:互联网 发布:淘宝凌美钢笔店铺推荐 编辑:程序博客网 时间:2024/06/04 19:15
在QML应用设计中,C++在很多的时候作为一个语言来为应用做一些需要计算或拓展QML功能的选项.在今天的例程中,我们来介绍如何连接QML代码中的信号.具体更多的阅读,可以参阅文章"Interacting with QML Objects from C++".
为了说明问题的方便,我们还是来贴上我们的代码:
myclass.h
#ifndef MYCLASS_H#define MYCLASS_H#include <QObject>#include <QQuickItem>#include <QDebug>class MyClass : public QObject{ Q_OBJECTpublic: 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();}
signal qmlSignal(string msg) signal qmlSignalGeneric(var anObject)
我们的Main.qml设计也非常简单:
Main.qml
import QtQuick 2.0import 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
0 0
- 如何在C++代码中连接QML代码中的信号
- 如何在qmake项目中在QML语言中调用C++代码
- 如何在VS2010中调试c代码
- 如何在C++代码中遍历QML Item并修改它的属性
- BB10 Cascades: 如何在QML中访问C++中的数据。
- 如何在QML中调用C++的方法并接收C++的信号
- 如何在QML中调用C++的方法并接收C++的信号
- 如何在QML中调用C++的方法并接收C++的信号
- 如何在QML应用中得到一个Item的所有属性,信号及方法
- 布局xml中的属性设置如何在代码中实现
- 如何在logcat中显示AOSP代码中的打印信息
- 如何在C#代码中调用exe执行文件?
- 如何在C#代码中调用exe执行文件
- 如何在C++中调用C的代码
- 如何在C++中调用C的代码
- 如何在C++中调用C的代码
- 如何在C++中调用C的代码
- 如何在C代码中调用C++非成员函数
- 遇到的句子
- textarea实现换行
- final,finally和finallize的区别&&override和overload的区别
- qemu-kvm 中断虚拟化
- 导出excel
- 如何在C++代码中连接QML代码中的信号
- JavaScript 错误 - throw、try 和 catch
- ajax之判断用户名是否被注册
- NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
- sencha/extjs 动态创建表格
- 监听Web容器启动与关闭
- android 开源网站
- 计算两个日期之间的工作天数
- erlang ets表的使用