感觉qml,有利有弊,编写业务逻辑,使用起来感觉不爽。
qml界面语言与qt c++交互麻烦。c++到qml都要qmlRegisterType方式, setContextProperty 方式。
qml到c++,就像解析xml一样,找啊找啊
1,qml调用c++ qmlRegisterType方式
#include "mainwindow.h"
#include <QApplication>
#include <QScrollArea>
#include <QPushButton>
#include <QLabel>
#include <QImage>
#include <QQmlEngine>
#include <QtQml>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>
#include <QMetaObject>
#include <QDebug>
#include <QColor>
#include <QVariant>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
QQmlApplicationEngine engine;
//engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));
return app.exec();
}
class ColorMaker : public QObject
{
Q_OBJECT
public:
int nn = 0;
Q_INVOKABLE int setA(int a)
{
nn = a;
return nn;
}
signals:
void colorChanged(const QColor & color);
void currentTime(const QString &strTime);
public slots:
void start()
{
qDebug() << "qqqq" << nn;
}
void stop()
{
}
};
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
import an.qt.ColorMaker 1.0
Window {
id:rootid
objectName: "rootid";
visible: true
width: 640
height: 480
title: qsTr("Hello World qt5")
color: "gray"
Item {
ColorMaker {
id:colorMaker;
}
width: 200
height: 200
visible: true
layer.enabled: true
id: nonLayered
focus: true
Keys.enabled: true
Keys.onPressed: {
var component;
var sprite;
if(focus == true)
{
console.log("mouse button onSpacePressed 1")
var n = colorMaker.setA(100);
console.log("mouse button onSpacePressed " + n );
colorMaker.start();
}
else
console.log("mouse button onSpacePressed")
}
}
Text {
objectName: "textID";
id:textID
x:100
y:100
font { pixelSize: 19; bold: true }
text: "dddddddddddddddd"
}
}
2,qml调用c++ setContextProperty
修改这里
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4
//import an.qt.ColorMaker 1.0
Window {
id:rootid
objectName: "rootid";
visible: true
width: 640
height: 480
title: qsTr("Hello World qt5")
color: "gray"
Item {
/*ColorMaker {
id:colorMaker;
}*/
width: 200
height: 200
visible: true
3,c++调用qml
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//qmlRegisterType<ColorMaker>("an.qt.ColorMaker", 1, 0, "ColorMaker");
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("colorMaker", new ColorMaker);
engine.load(QUrl(QStringLiteral("qrc:/cpptoQml.qml")));
QObject * rootItem = 0;
QList<QObject*> listQ = engine.rootObjects();
for( QList<QObject*>::iterator it = listQ.begin();it != listQ.end();++it)
{
qDebug() << " return - " << (*it)->objectName();
if( (*it)->objectName() == "rootid")
{
rootItem = *it;
break;
}
}
if(!rootItem)
return 0;
QObject * textLabel = rootItem->findChild<QObject*>("textID");
if(textLabel)
{
bool bRet = QMetaObject::invokeMethod(textLabel, "setText", Q_ARG(QString, "world hello"));
qDebug() << "call setText return - " << bRet;
textLabel->setProperty("color", QColor::fromRgb(255,0,0));
textLabel->setProperty("text", QString("world hello"));
bRet = QMetaObject::invokeMethod(textLabel, "doLayout");
qDebug() << "call doLayout return - " << bRet;
}
return app.exec();
}