1、定义业务C++类
#ifndef BUSINESS_H
#define BUSINESS_H
#include <QObject>
class Business : public QObject
{
Q_OBJECT
public:
explicit Business(QObject *parent = nullptr);
Q_PROPERTY(int mode READ getMode WRITE setMode NOTIFY sigModeChanged);//定义属性
public:
//setts&getters
void setMode(int);
int getMode();
public slots:
//直接可以在QML里调用的接口
Q_INVOKABLE void stop();
Q_INVOKABLE void start();
signals:
void sigModeChanged();
void sigTimeChanged(QString tt);
private:
int mode_;
class QTimer *timer_;
};
#endif // BUSINESS_H
#include "business.h"
#include <QTimer>
#include <QDateTime>
Business::Business(QObject *parent)
: QObject(parent)
,mode_(0)
{
timer_ = new QTimer(this);
connect(timer_,&QTimer::timeout,this,[this](){
QString timestr;
//不同的模式
switch (mode_) {
case 0:
timestr = QDateTime::currentDateTime().toString("MM-dd-yyyy hh:mm:ss");
break;
case 1:
timestr = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
break;
default:
timestr = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
break;
}
sigTimeChanged(QString("Mode%1:").arg(mode_) + timestr);
});
}
void Business::setMode(int m)
{
if(mode_ != m)
{
mode_ = m;
sigModeChanged();
}
}
int Business::getMode()
{
return mode_;
}
void Business::stop()
{
timer_->stop();
}
void Business::start()
{
timer_->start(1000);
}
2、定义QML
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import Business.test 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("QML Interactive With C++ Test ")
property alias value : id_txt.text
Rectangle{
id:jeams
width: 300
height: 40
anchors.horizontalCenter: parent.horizontalCenter
Text {
id:id_txt
anchors.centerIn: parent
text: qsTr("QML Interactive With C++")
color: "black"
}
color: "yellow"
}
Button{
id:btnstart
width: 80
height: 40
text: "开始"
anchors.centerIn: parent
anchors.horizontalCenterOffset: -40
onClicked: {
id_business.start()
}
}
Button{
id:btnstop
width: 80
height: 40
text: "停止"
anchors.centerIn: parent
anchors.horizontalCenterOffset: 40
onClicked: {
id_business.stop()
}
}
Button{
id:leftbtn
width: 80
height: 40
text: "模式0"
anchors.left: parent.left
anchors.bottom: parent.bottom
onClicked: {
id_business.mode = 0
}
}
Button{
id:centerbtn
width: 80
height: 40
text: "模式1"
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
onClicked: {
id_business.mode = 1
}
}
Button{
id:rightbtn
width: 80
height: 40
text: "模式2"
anchors.right: parent.right
anchors.bottom: parent.bottom
onClicked: {
id_business.mode = 2
}
}
MyBusiness{
id: id_business
onSigTimeChanged:{
value = tt
}
}
}
3、注册
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "business.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<Business>("Business.test",1,0,"MyBusiness");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
4、效果