标签:gui 定时 context date this prot efi mic net
Qt Quick 技术的引入。使得你能够高速构建 UI ,具有动画、各种绚丽效果的 UI 都不在话下。但它不是万能的。也有非常多局限性,原来 Qt 的一些技术,比方低阶的网络编程如 QTcpSocket ,多线程,又如 XML 文档处理类库 QXmlStreamReader / QXmlStreamWriter 等等,在 QML 中要么不可用,要么用起来不方便,所以呢,非常多时候我们是会基于这样的原则来混合使用 QML 和 C++: QML 构建界面。 C++ 实现非界面的业务逻辑和复杂运算。
版权全部 foruok 。转载请注明出处:http://blog.csdn.net/foruok 。
那这篇呢,我们就来看看 QML 和 C++ 之间怎样交互。
事实上话说回来, QML 的非常多基本类型原本就是在 C++ 中实现的。比方 Item 相应 QQuickItem 。 Image 相应 QQuickImage , Text 相应 QQuickText ,……这样看来。在 QML 中訪问 C++ 对象必定不成问题。
然也!
反过来,在 C++ 中事实上也能够使用 QML 对象。
对于这两种情景,我们分别讲述。
先看怎样在 QML 中使用 C++ 类和对象。
首先我们须要创建一个 Qt Quick App ,请參考《Qt Quick 之 Hello World 图文具体解释》建立一个名为 colorMaker 的项目。接下来我们的学习将会伴随 colorMaker 项目进行,等我们讲完。一个完整的 colorMaker 项目也会完毕。须要新建两个文件, colorMaker.h 和 colorMaker.cpp 。
colorMaker 仅仅是一个演示样例项目,我在 C++ 中实现一个 ColorMaker 类,它能够被注冊为一个 QML 类型供 QML 像内建类型一样使用。它的实例也能够导出为 QML 上下文属性在 QML 中訪问。我们的演示样例仅仅是在界面顶部显示当前时间(时间文字的颜色随时间变化而变化),在界面中间显示一个变色矩形。在界面底部放置几个按钮来控制颜色怎样变化。
图 1 是效果图:
图 1 colorMaker 效果图
我们知道。 QML 事实上是对 JavaScript 的扩展。融合了 Qt Object 系统,它是一种新的解释型的语言。 QML 引擎尽管由 Qt C++ 实现,但 QML 对象的执行环境。说究竟和 C++ 对象的上下文环境是不同的,是平行的两个世界。假设你想在 QML 中訪问 C++ 对象。那么必定要找到一种途径来在两个执行环境之间建立沟通桥梁。
Qt 提供了两种在 QML 环境中使用 C++ 对象的方式:
无论哪种方式。对要导出的 C++ 类都有要求,不是一个类的全部方法、变量都能够被 QML 使用。因此我们先来看看怎样让一个方法或属性能够被 QML 使用。
class ColorMaker : public QObject { Q_OBJECT public: ColorMaker(QObject *parent = 0); ~ColorMaker(); signals: void colorChanged(const QColor & color); void currentTime(const QString &strTime); public slots: void start(); void stop(); };
class ColorMaker : public QObject { Q_OBJECT public: ColorMaker(QObject *parent = 0); ~ColorMaker(); Q_INVOKABLE GenerateAlgorithm algorithm() const; Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm); signals: void colorChanged(const QColor & color); void currentTime(const QString &strTime); public slots: void start(); void stop(); };
Component.onCompleted: { colorMaker.color = Qt.rgba(0,180,120, 255); colorMaker.setAlgorithm(ColorMaker.LinearIncrease); changeAlgorithm(colorAlgorithm, colorMaker.algorithm()); }
class ColorMaker : public QObject { Q_OBJECT Q_ENUMS(GenerateAlgorithm) public: ColorMaker(QObject *parent = 0); ~ColorMaker(); enum GenerateAlgorithm{ RandomRGB, RandomRed, RandomGreen, RandomBlue, LinearIncrease }; Q_INVOKABLE GenerateAlgorithm algorithm() const; Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm); signals: void colorChanged(const QColor & color); void currentTime(const QString &strTime); public slots: void start(); void stop(); };
要想使用 Q_PROPERTY 宏,你的类必须是 QObject 的后裔,必须在类首使用 Q_OBJECT 宏。
Q_PROPERTY(type name (READ getFunction [WRITE setFunction] | MEMBER memberName [(READ getFunction | WRITE setFunction)]) [RESET resetFunction] [NOTIFY notifySignal] [REVISION int] [DESIGNABLE bool] [SCRIPTABLE bool] [STORED bool] [USER bool] [CONSTANT] [FINAL])
只是。不是全部的选项都必须设定,看一个最简短的属性声明:
Q_PROPERTY(int x READ x)
它指定的函数,仅仅能有一个与属性类型匹配的參数,必须返回 void 。
class QQuickText : public QQuickImplicitSizeItem { Q_OBJECT Q_ENUMS(HAlignment) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) ... public: enum HAlignment { AlignLeft = Qt::AlignLeft, AlignRight = Qt::AlignRight, AlignHCenter = Qt::AlignHCenter, AlignJustify = Qt::AlignJustify }; ... QString text() const; void setText(const QString &); QFont font() const; void setFont(const QFont &font); QColor color() const; void setColor(const QColor &c); ... };
class ColorMaker : public QObject { Q_OBJECT Q_ENUMS(GenerateAlgorithm) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) Q_PROPERTY(QColor timeColor READ timeColor) public: ColorMaker(QObject *parent = 0); ~ColorMaker(); enum GenerateAlgorithm{ RandomRGB, RandomRed, RandomGreen, RandomBlue, LinearIncrease }; QColor color() const; void setColor(const QColor & color); QColor timeColor() const; Q_INVOKABLE GenerateAlgorithm algorithm() const; Q_INVOKABLE void setAlgorithm(GenerateAlgorithm algorithm); signals: void colorChanged(const QColor & color); void currentTime(const QString &strTime); public slots: void start(); void stop(); protected: void timerEvent(QTimerEvent *e); private: GenerateAlgorithm m_algorithm; QColor m_currentColor; int m_nColorTimer; };
#include "colorMaker.h" #include <QTimerEvent> #include <QDateTime> ColorMaker::ColorMaker(QObject *parent) : QObject(parent) , m_algorithm(RandomRGB) , m_currentColor(Qt::black) , m_nColorTimer(0) { qsrand(QDateTime::currentDateTime().toTime_t()); } ColorMaker::~ColorMaker() { } QColor ColorMaker::color() const { return m_currentColor; } void ColorMaker::setColor(const QColor &color) { m_currentColor = color; emit colorChanged(m_currentColor); } QColor ColorMaker::timeColor() const { QTime time = QTime::currentTime(); int r = time.hour(); int g = time.minute()*2; int b = time.second()*4; return QColor::fromRgb(r, g, b); } ColorMaker::GenerateAlgorithm ColorMaker::algorithm() const { return m_algorithm; } void ColorMaker::setAlgorithm(GenerateAlgorithm algorithm) { m_algorithm = algorithm; } void ColorMaker::start() { if(m_nColorTimer == 0) { m_nColorTimer = startTimer(1000); } } void ColorMaker::stop() { if(m_nColorTimer > 0) { killTimer(m_nColorTimer); m_nColorTimer = 0; } } void ColorMaker::timerEvent(QTimerEvent *e) { if(e->timerId() == m_nColorTimer) { switch(m_algorithm) { case RandomRGB: m_currentColor.setRgb(qrand() % 255, qrand() % 255, qrand() % 255); break; case RandomRed: m_currentColor.setRed(qrand() % 255); break; case RandomGreen: m_currentColor.setGreen(qrand() % 255); break; case RandomBlue: m_currentColor.setBlue(qrand() % 255); break; case LinearIncrease: { int r = m_currentColor.red() + 10; int g = m_currentColor.green() + 10; int b = m_currentColor.blue() + 10; m_currentColor.setRgb(r % 255, g % 255, b % 255); } break; } emit colorChanged(m_currentColor); emit currentTime(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")); } else { QObject::timerEvent(e); } }
template<typename T> int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName); template<typename T, int metaObjectRevision> int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName);
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QtQml> #include "colorMaker.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker"); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/colorMaker/main.qml")); viewer.showExpanded(); return app.exec(); }
import an.qt.ColorMaker 1.0
Rectangle { width: 360; height: 360; ColorMaker { id: colorMaker; color: Qt.green; } }
假设你不记得 Qt Quick 的基本元素,看《Qt Quick 简单教程》吧。
import QtQuick 2.0 import QtQuick.Controls 1.1 import an.qt.ColorMaker 1.0 Rectangle { width: 360; height: 360; Text { id: timeLabel; anchors.left: parent.left; anchors.leftMargin: 4; anchors.top: parent.top; anchors.topMargin: 4; font.pixelSize: 26; } ColorMaker { id: colorMaker; color: Qt.green; } Rectangle { id: colorRect; anchors.centerIn: parent; width: 200; height: 200; color: "blue"; } Button { id: start; text: "start"; anchors.left: parent.left; anchors.leftMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; onClicked: { colorMaker.start(); } } Button { id: stop; text: "stop"; anchors.left: start.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { colorMaker.stop(); } } function changeAlgorithm(button, algorithm){ switch(algorithm) { case 0: button.text = "RandomRGB"; break; case 1: button.text = "RandomRed"; break; case 2: button.text = "RandomGreen"; break; case 3: button.text = "RandomBlue"; break; case 4: button.text = "LinearIncrease"; break; } } Button { id: colorAlgorithm; text: "RandomRGB"; anchors.left: stop.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { var algorithm = (colorMaker.algorithm() + 1) % 5; changeAlgorithm(colorAlgorithm, algorithm); colorMaker.setAlgorithm(algorithm); } } Button { id: quit; text: "quit"; anchors.left: colorAlgorithm.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { Qt.quit(); } } Component.onCompleted: { colorMaker.color = Qt.rgba(0,180,120, 255); colorMaker.setAlgorithm(ColorMaker.LinearIncrease); changeAlgorithm(colorAlgorithm, colorMaker.algorithm()); } Connections { target: colorMaker; onCurrentTime:{ timeLabel.text = strTime; timeLabel.color = colorMaker.timeColor; } } Connections { target: colorMaker; onColorChanged:{ colorRect.color = color; } } }
这里使用 ColorMaker 的 timeColor 属性。该属性的读取函数是 timeColor 。回看一下 colorMaker.cpp 中的实现:
QColor ColorMaker::timeColor() const { QTime time = QTime::currentTime(); int r = time.hour(); int g = time.minute()*2; int b = time.second()*4; return QColor::fromRgb(r, g, b); }
我们定义的 changeAlgorithm 函数,接受两个參数, button 和 algorithm 。假设你是 C++ 程序员。可能有点儿不适应:怎么參数就木有类型呢哈…… JavaScript 就是酱紫滴,拥有动态类型,一个变量在赋值时决定其类型。
我们还是以 colorMaker 为例。对其代码做适当改动来适应本节的内容。
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QtQml> #include "colorMaker.h" int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.rootContext()->setContextProperty("colorMaker", new ColorMaker); viewer.setMainQmlFile(QStringLiteral("qml/colorMaker/main.qml")); viewer.showExpanded(); return app.exec(); }
viewer.rootContext()->setContextProperty("colorMaker", new ColorMaker);
ReferenceError: ColorMaker is not defined
import QtQuick 2.0 import QtQuick.Controls 1.1 //[1] //import an.qt.ColorMaker 1.0 Rectangle { width: 360; height: 360; Text { id: timeLabel; anchors.left: parent.left; anchors.leftMargin: 4; anchors.top: parent.top; anchors.topMargin: 4; font.pixelSize: 26; } /* [2] ColorMaker { id: colorMaker; color: Qt.green; } */ Rectangle { id: colorRect; anchors.centerIn: parent; width: 200; height: 200; color: "blue"; } Button { id: start; text: "start"; anchors.left: parent.left; anchors.leftMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; onClicked: { colorMaker.start(); } } Button { id: stop; text: "stop"; anchors.left: start.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { colorMaker.stop(); } } function changeAlgorithm(button, algorithm){ switch(algorithm) { case 0: button.text = "RandomRGB"; break; case 1: button.text = "RandomRed"; break; case 2: button.text = "RandomGreen"; break; case 3: button.text = "RandomBlue"; break; case 4: button.text = "LinearIncrease"; break; } } Button { id: colorAlgorithm; text: "RandomRGB"; anchors.left: stop.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { var algorithm = (colorMaker.algorithm() + 1) % 5; changeAlgorithm(colorAlgorithm, algorithm); colorMaker.setAlgorithm(algorithm); } } Button { id: quit; text: "quit"; anchors.left: colorAlgorithm.right; anchors.leftMargin: 4; anchors.bottom: start.bottom; onClicked: { Qt.quit(); } } Component.onCompleted: { colorMaker.color = Qt.rgba(0,180,120, 255); //[3] //colorMaker.setAlgorithm(ColorMaker.LinearIncrease); colorMaker.setAlgorithm(2); changeAlgorithm(colorAlgorithm, colorMaker.algorithm()); } Connections { target: colorMaker; onCurrentTime:{ timeLabel.text = strTime; timeLabel.color = colorMaker.timeColor; } } Connections { target: colorMaker; onColorChanged:{ colorRect.color = color; } } }
而对于使用 Q_PROPERTY 定义的属性,能够使用 QObject 的 property() 方法訪问属性,假设该属性定义了 WRITE 方法。还能够使用 setProperty() 改动属性。
所以仅仅要我们找到 QML 环境中的某个对象,就能够通过元对象系统来訪问它的属性、信号、槽等。
如今该说到查找对象的方法了: findChild() 和 findChildren() 。它们的函数原型例如以下:
T QObject::findChild(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const; QList<T> QObject::findChildren(const QString & name = QString(), Qt::FindChildOptions options = Qt::FindChildrenRecursively) const; QList<T> QObject::findChildren(const QRegExp & regExp, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const; QList<T> QObject::findChildren(const QRegularExpression & re, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const;
QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
QList<QWidget *> widgets = parentWidget.findChildren<QWidget *>("widgetname");
bool QMetaObject::invokeMethod(QObject * obj, const char * member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0 = QGenericArgument( 0 ), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) [static]事实上 QMetaObject 还有三个 invokeMethod() 函数。只是都是上面这个原型的重载,所以我们仅仅要介绍上面这个就 OK 了。
嗯。看来信号与槽的參数个数是有限制的,不能超过 10 个。
QGenericArgument Q_ARG( Type, const Type & value)
QGenericReturnArgument Q_RETURN_ARG( Type, Type & value)
QString retVal; QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection, Q_RETURN_ARG(QString, retVal), Q_ARG(QString, "sqrt"), Q_ARG(int, 42), Q_ARG(double, 9.7));
QMetaObject::invokeMethod(thread, "quit", Qt::QueuedConnection);
main.qml 内容例如以下:
import QtQuick 2.0 import QtQuick.Controls 1.1 Rectangle { objectName: "rootRect"; width: 360; height: 360; Text { objectName: "textLabel"; text: "Hello World"; anchors.centerIn: parent; font.pixelSize: 26; } Button { anchors.right: parent.right; anchors.rightMargin: 4; anchors.bottom: parent.bottom; anchors.bottomMargin: 4; text: "quit"; objectName: "quitButton"; } }
#include <QtGui/QGuiApplication> #include "qtquick2applicationviewer.h" #include <QQuickItem> #include "changeColor.h" #include <QMetaObject> #include <QDebug> #include <QColor> #include <QVariant> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QtQuick2ApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/callQml/main.qml")); viewer.showExpanded(); QQuickItem * rootItem = viewer.rootObject(); new ChangeQmlColor(rootItem); QObject * quitButton = rootItem->findChild<QObject*>("quitButton"); if(quitButton) { QObject::connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit())); } QObject *textLabel = rootItem->findChild<QObject*>("textLabel"); if(textLabel) { //1. failed call bool bRet = QMetaObject::invokeMethod(textLabel, "setText", Q_ARG(QString, "world hello")); qDebug() << "call setText return - " << bRet; textLabel->setProperty("color", QColor::fromRgb(255,0,0)); bRet = QMetaObject::invokeMethod(textLabel, "doLayout"); qDebug() << "call doLayout return - " << bRet; } return app.exec(); }
Starting D:\projects\...\release\callQml.exe... QMetaObject::invokeMethod: No such method QQuickText::setText(QString) call setText return - false call doLayout return - true
new ChangeQmlColor(rootItem);
#ifndef CHANGECOLOR_H #define CHANGECOLOR_H #include <QObject> #include <QTimer> class ChangeQmlColor : public QObject { Q_OBJECT public: ChangeQmlColor(QObject *target, QObject *parent = 0); ~ChangeQmlColor(); protected slots: void onTimeout(); private: QTimer m_timer; QObject *m_target; }; #endif
#include "changeColor.h" #include <QDateTime> #include <QColor> #include <QVariant> ChangeQmlColor::ChangeQmlColor(QObject *target, QObject *parent) : QObject(parent) , m_timer(this) , m_target(target) { qsrand(QDateTime::currentDateTime().toTime_t()); connect(&m_timer, SIGNAL(timeout()), this, SLOT(onTimeout())); m_timer.start(1000); } ChangeQmlColor::~ChangeQmlColor() {} void ChangeQmlColor::onTimeout() { QColor color = QColor::fromRgb(qrand()%256, qrand()%256, qrand()%256); m_target->setProperty("color", color); }
版权全部 foruok 。转载请注明出处:http://blog.csdn.net/foruok 。
标签:gui 定时 context date this prot efi mic net
原文地址:http://www.cnblogs.com/cynchanpin/p/7093050.html