• Q_PROPERTY 中notify关键词使用


    The Property System属性系统介绍:

    A NOTIFY signal is optional. If defined, it should specify one existing signal in that class that is emitted whenever the value of the property changes. NOTIFY signals for MEMBER variables must take zero or one parameter, which must be of the same type as the property. The parameter will take the new value of the property. The NOTIFY signal should only be emitted when the property has really been changed, to avoid bindings being unnecessarily re-evaluated in QML, for example. Qt emits automatically that signal when needed for MEMBER properties that do not have an explicit setter. 

    NOTIRY是可选的,它指定一个信号函数,只要属性变化,那么该类就要发出信号。使用MEMBER修饰的变量,信号要么0个,要么1个与变量类型相同的参数,NOTIFY信号,只因该在变量值变化后发出。避免在QML中绑定重复的评估(大概意思属性绑定的重复更新),例如通过MEMBER修饰,且没有显示的设置函数(WRITER),那么Qt将会自动发出NOTIFY信号。

    实践过程中:
    使用MEMBER,且没有使用WRITER,发现,QML能主动发出NOTIFY信号。但c++类中,不能主动发出NOTIFY信号。

    代码:

    1. #ifndef SIMPLE_H
    2. #define SIMPLE_H
    3. #include
    4. class Simple : public QObject
    5. {
    6. Q_OBJECT
    7. Q_PROPERTY(QString name MEMBER m_name NOTIFY nameChanged)
    8. public:
    9. explicit Simple(QObject *parent = nullptr);
    10. public:
    11. // QString name(){ return m_name;}
    12. // void setName(QString name)
    13. // {
    14. // m_name = name;
    15. // //emit nameChanged(name);
    16. // }
    17. public slots:
    18. void slotNameChanged(QString name);
    19. void slotTimeOut();
    20. signals:
    21. void nameChanged(QString name);
    22. private:
    23. QString m_name;
    24. };
    25. #endif // SIMPLE_H
    26. #include "Simple.h"
    27. #include "QDebug"
    28. #include
    29. Simple::Simple(QObject *parent)
    30. : QObject{parent}
    31. {
    32. m_name = "hello";
    33. connect(this,SIGNAL(nameChanged(QString)),this,SLOT(slotNameChanged(QString)));
    34. QTimer *timer = new QTimer(this);
    35. timer->setInterval(2000);
    36. connect(timer,SIGNAL(timeout()),this,SLOT(slotTimeOut()));
    37. timer->start();
    38. }
    39. void Simple::slotNameChanged(QString name)
    40. {
    41. qDebug() << "name Changed name" << name;
    42. }
    43. void Simple::slotTimeOut()
    44. {
    45. qDebug() << "slotTimeOut!!!" << m_name;
    46. m_name = "world";
    47. emit nameChanged(m_name);
    48. }
    1. #include
    2. #include
    3. #include
    4. #include "Simple.h"
    5. int main(int argc, char *argv[])
    6. {
    7. qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    8. QGuiApplication app(argc, argv);
    9. QQmlApplicationEngine engine;
    10. Simple *simple = new Simple();
    11. engine.rootContext()->setContextProperty("Simple",simple);
    12. const QUrl url(u"qrc:/untitled1/main.qml"_qs);
    13. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
    14. &app, [url](QObject *obj, const QUrl &objUrl) {
    15. if (!obj && url == objUrl)
    16. QCoreApplication::exit(-1);
    17. }, Qt::QueuedConnection);
    18. engine.load(url);
    19. return app.exec();
    20. }
    1. //main.xml
    2. import QtQuick
    3. import QtQuick.VirtualKeyboard
    4. import QtQuick.Controls
    5. import QtQuick.Layouts
    6. Window {
    7. id: window
    8. width: 300
    9. height: 300
    10. visible: true
    11. title: Simple.name
    12. Button
    13. {
    14. anchors.fill:parent
    15. onClicked:
    16. {
    17. Simple.name = "hello";
    18. console.log("click!!!");
    19. }
    20. }
    21. }

  • 相关阅读:
    【MySQL】数据库中表的增删查改操作详解
    化合物在高通量筛选中的作用
    从0开始刷力扣
    【微信小程序】项目实战—抽签应用
    移动端签名组件封装 借用插件 vue-esign
    jmeter单接口和多接口测试
    搭建搜题公众号【最新】
    Spring-IOC 控制反转
    Node.js-express框架-cookie设置参数详解和举例
    Selenium二次封装与网络测试工具开发
  • 原文地址:https://blog.csdn.net/qq_39280795/article/details/126918575