QML与C 混合编程
使用QQuickView
pro文件中添加quick模块
代码语言:javascript复制#include<QApplication>
#include<QQuickView>
int main(int argc,char* argv[]){
QApplication app(argc,argv);
//加载qml文件到视图
QQuickView view;
view.setSource(QUrl("column.qml"));
view.show();
return app.exec();
}
使用QQmlApplicationEngine
创建Qt Quick Application
代码语言:javascript复制#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
//支持高频率屏幕
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
//创建qml引擎
QQmlApplicationEngine engine;
//引擎加载qml文件
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
QML调用C 函数
代码语言:javascript复制#ifndef APPLICATIONDATA_H
#define APPLICATIONDATA_H
#include <QObject>
#include <QDateTime>
class ApplicationData : public QObject
{
Q_OBJECT
public:
explicit ApplicationData(QObject *parent = nullptr);
//QML中调用C 函数,这个函数需以Q_INVOKABLE进行标记——注册一个方法到QML
//或者该函数是Qt的槽函数
Q_INVOKABLE QDateTime getCurrentDateTime()const{
return QDateTime::currentDateTime();
}
signals:
public slots:
};
#endif // APPLICATIONDATA_H
代码语言:javascript复制#include <QApplication>
#include <QQuickView>
#include "applicationdata.h"
#include <QQmlContext>
int main(int argc,char* argv[]){
QApplication app(argc,argv);
QQuickView view;
//将C 对象作为属性注册到QML
ApplicationData data;
view.rootContext()->setContextProperty("applicationData",&data);
view.setSource(QUrl("item.qml"));
view.show();
return app.exec();
}
代码语言:javascript复制import QtQuick 2.0
Text{
text: applicationData.getCurrentDateTime()
}
C 调用QML函数
代码语言:javascript复制import QtQuick 2.0
Text{
text: applicationData.getCurrentDateTime()
function qmlFunction(msg){
console.log("Message comes:",msg)
return "abc"
}
}
代码语言:javascript复制#include <QApplication>
#include <QQuickView>
#include "applicationdata.h"
#include <QQmlContext>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
int main(int argc,char* argv[]){
QApplication app(argc,argv);
QQmlApplicationEngine engine;
QQmlComponent component(&engine,"item.qml");
QObject* object = component.create();
QVariant msg = "Hello qml!";
QVariant returnedValue;
//invokeMethod的 Q_RETURN_ARG Q_ARG参数必须指定为QVariant类型
QMetaObject::invokeMethod(
object,
"qmlFunction", Q_RETURN_ARG(QVariant,returnedValue),//用于接收返回值
Q_ARG(QVariant,msg));//用于传递函数函数
qDebug()<<"QML function returned:"<<returnedValue.toString();
return app.exec();
}
C 接收QML信号
代码语言:javascript复制import QtQuick 2.0
Item {
id:item
width:100
height: 100
signal qmlSignal(string msg)
MouseArea{
anchors.fill: parent
onClicked: item.qmlSignal("Hello qml")
}
}
代码语言:javascript复制#include <QApplication>
#include <QQuickView>
#include<QQuickItem>
#include "myclass.h"
int main(int argc,char* argv[]){
QApplication app(argc,argv);
QQuickView view(QUrl::fromLocalFile("quicksignal.qml"));
QObject* item = view.rootObject();
MyClass myClass;
QObject::connect(item,
SIGNAL(qmlSignal(QString)),
&myClass,
SLOT(slotPrint(QString)));
view.show();
return app.exec();
}
代码语言:javascript复制#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = nullptr);
signals:
public slots:
void slotPrint(QString s){
qDebug()<<s;
}
};
#endif // MYCLASS_H