Results 1 to 3 of 3

Thread: Can't get the notify signal in QML

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2020
    Posts
    2
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Can't get the notify signal in QML

    Hi. I have a problem.
    For some reason, the Q_PROPERTY NOTIFY does not work in QML. I do understand, that i'm missing something, but don't get what for a 3rd day

    1. I have two classes, one is BoardWiring, second is MainUi.
    2. In MainUi - i have one integer, called minTimer, with the NOTIFY onMinTimerChanged().
    3. The classes BoardWiring and MainUi are added in main.cpp, to use in QML.
    4. The MainUi is added in private section of BoardWiring.
    5. Then, I call BoardWiring.start() from QML, which calls MainUi.startTimer(), which suppose to change the static MainUi.minTimer integer to 1.
    5. The MainUi.minTimer is changed, but i can't receive the NOTIFY signal in QML, it's just not notified minTimerElement.

    Can someone help me with this please?


    Here's the minimum of the code to reproduce:

    main.cpp

    Qt Code:
    1. #include <QDebug>
    2. #include <QGuiApplication>
    3. #include <QQmlApplicationEngine>
    4. #include <QQmlContext>
    5. #include <QQuickView>
    6. #include <QSettings>
    7.  
    8. #include "boardwiring.h"
    9. #include "mainui.h"
    10. #include <QString>
    11. #include <stdint.h>
    12. #include <stdio.h>
    13. #include <stdlib.h>
    14.  
    15. int main(int argc, char *argv[]) {
    16.  
    17. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    18. QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
    19.  
    20. QGuiApplication app(argc, argv);
    21.  
    22. QtWebEngine::initialize();
    23.  
    24. QCoreApplication::setOrganizationName("Test");
    25. QCoreApplication::setOrganizationDomain("Test");
    26. QCoreApplication::setApplicationName("Test");
    27.  
    28. // qmlRegisterType<BoardWiring>("io.qt.boardwiring", 1, 0, "BoardWiring");
    29. // qmlRegisterType<MainUi>("io.qt.mainui", 1, 0, "MainUi");
    30.  
    31. BoardWiring boardwiring;
    32. MainUi mainui;
    33.  
    34. QQmlApplicationEngine engine;
    35.  
    36. engine.rootContext()->setContextProperty("boardwiring", &boardwiring);
    37. engine.rootContext()->setContextProperty("mainui", &mainui);
    38.  
    39. engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
    40.  
    41. return app.exec();
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

    Main.qml

    Qt Code:
    1. import QtQuick 2.12
    2. import QtQuick.Controls 2.9
    3. import QtQuick.Controls.Imagine 2.4
    4. import QtQuick.Window 2.3
    5.  
    6. ApplicationWindow {
    7. id: window
    8. visible: true
    9. width: 1280
    10. height: 600
    11. title: qsTr("Test")
    12.  
    13. RoundButton {
    14. id: startButton
    15. x: 14
    16. y: 26
    17. width: 230
    18. height: 230
    19. text: !boardwiring.isStarted ? "Start" : "Stop"
    20. font.pointSize: 30
    21. background: Image { source: boardwiring.isStarted? "/stop.png" : "/Start.png"; }
    22. onClicked: {
    23. if(!boardwiring.isStarted) {
    24. boardwiring.start();
    25. console.log("Main ui: " + mainui.minTimer);
    26. } else {
    27. boardwiring.stop();
    28. console.log("Main ui: " + mainui.minTimer);
    29. }
    30.  
    31. }
    32.  
    33. contentItem: Text {
    34. text: startButton.text
    35. font: startButton.font
    36. opacity: 1.0
    37. color: "black"
    38. horizontalAlignment: Text.AlignHCenter
    39. verticalAlignment: Text.AlignVCenter
    40. }
    41. }
    42.  
    43. Text {
    44. id: minTimerElement
    45. x: 979
    46. y: 291
    47. text: mainui.minTimer // Not notifies it!
    48. font.pixelSize: 16
    49. }
    50.  
    51.  
    52. }
    To copy to clipboard, switch view to plain text mode 

    boardwiring.h
    Qt Code:
    1. #ifndef BOARDWIRING_H
    2. #define BOARDWIRING_H
    3.  
    4. #include "mainui.h"
    5. #include <QObject>
    6. #include <QString>
    7.  
    8. class BoardWiring : public QObject {
    9. Q_OBJECT
    10.  
    11. Q_PROPERTY(bool isStarted READ isStarted NOTIFY isStartedChanged WRITE
    12. setIsStarted)
    13.  
    14. public:
    15. explicit BoardWiring(QObject *parent = nullptr);
    16.  
    17. bool isStarted();
    18.  
    19. Q_INVOKABLE void start();
    20. Q_INVOKABLE void setIsStarted(bool &isStarted);
    21.  
    22.  
    23. signals:
    24. void isStartedChanged(bool b_isStarted);
    25.  
    26.  
    27. public slots:
    28.  
    29.  
    30. private:
    31. MainUi mainUi;
    32. static bool b_isStarted;
    33. };
    34.  
    35. #endif // BOARDWIRING_H
    To copy to clipboard, switch view to plain text mode 

    boardwiring.cpp
    Qt Code:
    1. #include "boardwiring.h"
    2. #include <QDebug>
    3. #include <QString>
    4.  
    5. #include <stdint.h>
    6. #include <stdio.h>
    7. #include <stdlib.h>
    8.  
    9. bool BoardWiring::b_isStarted = 0;
    10.  
    11. bool BoardWiring::isStarted() { return b_isStarted; }
    12. void BoardWiring::setIsStarted(bool &isStarted) {
    13. b_isStarted = isStarted;
    14. emit isStartedChanged(isStarted);
    15. }
    16.  
    17. BoardWiring::BoardWiring(QObject *parent) : QObject(parent) {}
    18.  
    19. void BoardWiring::start() {
    20.  
    21.  
    22. if (!b_isStarted) {
    23. qDebug() << "STARTED";
    24. b_isStarted = true;
    25.  
    26. mainUi.startTimer(); // Here i call the startTimer
    27. }
    28.  
    29. emit isStartedChanged(b_isStarted);
    30. }
    31.  
    32.  
    33. void BoardWiring::stop() {
    34.  
    35. b_isStarted = false;
    36.  
    37. mainUi.stopTimer();
    38.  
    39. emit isStartedChanged(b_isStarted);
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    mainui.h
    Qt Code:
    1. #ifndef MAINUI_H
    2. #define MAINUI_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6.  
    7. class MainUi : public QObject {
    8. Q_OBJECT
    9.  
    10. Q_PROPERTY(
    11. int minTimer READ minTimer NOTIFY minTimerChanged WRITE setMinTimer)
    12.  
    13. public:
    14. explicit MainUi(QObject *parent = nullptr);
    15.  
    16. Q_INVOKABLE void startTimer();
    17.  
    18. int minTimer();
    19.  
    20. signals:
    21. void minTimerChanged(const int &m_minTimer);
    22.  
    23. public slots:
    24. void stopTimer();
    25. void setMinTimer(int &minTimer);
    26.  
    27.  
    28. private:
    29. static int m_minTimer;
    30. };
    31.  
    32. #endif // MAINUI_H
    To copy to clipboard, switch view to plain text mode 

    mainui.cpp
    Qt Code:
    1. #include "mainui.h"
    2. #include <QDebug>
    3. #include <QSettings>
    4. #include <QTimer>
    5.  
    6. int MainUi::m_minTimer = 0;
    7.  
    8. MainUi::MainUi(QObject *parent) : QObject(parent) {}
    9.  
    10. int MainUi::minTimer() { return m_minTimer; }
    11. void MainUi::setMinTimer(int &minTimer) {
    12. m_minTimer = minTimer;
    13. emit minTimerChanged(minTimer);
    14. }
    15.  
    16. void MainUi::startTimer() {
    17. m_minTimer = 1;
    18. setMinTimer(m_minTimer);
    19.  
    20. }
    21.  
    22. void MainUi::stopTimer() {
    23. m_minTimer = 0;
    24. setMinTimer(m_minTimer);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by iveus; 7th February 2020 at 19:16.

Similar Threads

  1. Qt notify badalloc
    By Sacha_D in forum Qt Programming
    Replies: 5
    Last Post: 13th November 2014, 13:18
  2. linux usb notify
    By duckshine in forum General Programming
    Replies: 1
    Last Post: 27th May 2011, 11:16
  3. how to implement QApplication.notify()
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 15th February 2010, 19:12
  4. QApplication::notify()
    By jeffpogo in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2009, 23:46
  5. Qt web notify
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 14:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.