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(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)
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; } }
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; } } }
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); }
#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; } } }
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 了。
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);
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。