
本文共 3334 字,大约阅读时间需要 11 分钟。
程序主要来自:
我只做了微小改动,具体工程创建步骤参考上面的链接。
完整工程:
链接:https://pan.baidu.com/s/1N5IMUua7jQTn-4z98WuhVQ
提取码:0000testproperty.h
#ifndef TESTPROPERTY_H#define TESTPROPERTY_H#includeclass TestProperty:public QObject{ Q_OBJECTpublic: explicit TestProperty(QObject *parent =nullptr); Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged); Q_PROPERTY(int num READ readNum WRITE setNum); QString title(); void setTitle(QString strTitle); int readNum(); void setNum(int Num);signals: void titleChanged();private: QString m_title; int num;};#endif // TESTPROPERTY_H
testproperty.cpp
#include "testproperty.h"TestProperty::TestProperty(QObject *parent):QObject(parent){}QString TestProperty::title(){ return m_title;}void TestProperty::setTitle(QString strTitle){ m_title=strTitle; emit titleChanged();}int TestProperty::readNum(){ return num;}void TestProperty::setNum(int Num){ num=Num;}
main.qml
import QtQuick 2.12import QtQuick.Window 2.12import TestProperty 2.0Window { id:root visible: true width: 640 height: 480 title: qsTr("Hello World") TestProperty{ id:testProperty title:qsTr("hello hht") num:10 onTitleChanged: { root.title=testProperty.title console.log("我被改变了") } } Component.onCompleted:{ title=testProperty.title } Rectangle{ width:20 height:20 color:"red" anchors.centerIn: parent MouseArea{ anchors.fill:parent onClicked:{ testProperty.title="change" } } }}
main.cpp
#include#include #include"testproperty.h"#include int main(int argc, char *argv[]){ QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); qmlRegisterType ("TestProperty",2,0,"TestProperty"); 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();}
说明
1.testproperty.h 中的 Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged); ,作用是使qml中的该类具有了title属性,title属性改变的时候,可以在onTitleChanged中捕获(前提是在cpp文件中,改变title时发射了titleChanged信号)。
第一个参数title是在qml中可以使用的属性,其类型是QString;
第二个参数是title,前面READ表明在qml中读title属性的时候,在C++中调用的函数名。比如,在main.qml中,执行root.title=testProperty.title时,便会执行restproperty.cpp中的QString TestProperty::title()函数,返回title内容,赋给root.title。
第三个参数是setTitle,前面WRITE表明给该属性写入值,比如,在main.qml中,声明TestProperty时写的title:qsTr("hello hht")以及在其他地方给该title赋值,比如testProperty.title="hello",这时便会调用estproperty.cpp中的void TestProperty::setTitle(QString strTitle)函数,给title赋值。
第四个参数titleChanged,前面NOTIFY表明这是一个信号,可以在qml中使用onTitleChanged:来捕获信号,前提是在C++中声明了该信号,比如在testproperty.h中,声明signals:void titleChanged();,并且在发射该信号后(比如在void TestProperty::setTitle(QString strTitle)中 emit titleChanged();),这个在qml中才可以捕获到。
2.qml程序的大致实现是:
(1)在窗口中央画了一个矩形;
(2)在这个矩形的MouseArea中,设置鼠标点击事件,改变estProperty的title;
(3)testProperty的title一改变,便会触发testProperty中的onTitleChanged;
(4)在onTitleChanged中,将窗口标题改变为刚修改的title值,并且输出文字“title被改变”。
发表评论
最新留言
关于作者
