Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Other class read unchanged variable value
QtWS25 Last Chance

Other class read unchanged variable value

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 808 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BD9a
    wrote on last edited by BD9a
    #1

    In my app I have 5 async loops. To keep GUI without laggs I made new Thread just for those loops. In config.h file b_drink variable and Q_PROPERTY to read and write to this variable from QML.

    Q_PROPERTY(bool b_drink READ r_drink WRITE w_drink);
    
    public:
        explicit Config(QObject *parent = nullptr);
        Q_PROPERTY(bool b_drink READ r_drink WRITE w_drink);
    
        bool b_drink = false;
        bool r_drink(){
            return b_drink;
        }
    
    public slots:
        void save();
        void load();
    
        void w_drink(bool value){    
            qDebug() << "Current value is: " << b_drink << ", changing to: " << value;
            b_drink = value;
        }
    
    Switch {
        id: control
        x: -13
        y: 43
        checked: Config.b_drink
        onClicked:{
            console.log(control.checked);
            Config.w_drink(control.checked);
        }
    }
    

    Logs from Application Output (false spam is from loop in other class and thread):

    QQmlExpression: Expression qrc:/main.qml:104:17 depends on non-NOTIFYable properties:
        Config::b_drink
    qrc:/main.qml:174:13: Unable to assign [undefined] to bool
    qrc:/main.qml:242:13: Unable to assign [undefined] to bool
    qrc:/main.qml:310:13: Unable to assign [undefined] to bool
    qrc:/main.qml:378:13: Unable to assign [undefined] to bool
    false
    false
    false
    qml: true
    Current value is:  false , changing to:  true
    false
    false
    false
    false
    false
    false
    false
    qml: false
    Current value is:  true , changing to:  false
    false
    qml: true
    Current value is:  false , changing to:  true
    false
    false
    
    1 Reply Last reply
    0
    • B BD9a

      @Christian-Ehrlicher how it should be done? In my opinion it should work (maybe cuz I dont understand it well?)

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #12

      @BD9a I'm not entirely sure, what's going, or to be precise why everything doesn't go up in flames, but it is running on my pc and it shoudn't.

      You can force this on your side as well by properly initializing the Config * inside Loop to a nullptr.

      private:
          Config *config = nullptr;
      
      

      that will crash, and it should.

      Here, what to do to fix this:

      Enable writing & storing

      void Config::w_drink(bool value)
      {
          qDebug() << "Current value is: " << b_drink;
          if( b_drink == value)
               return;
      
          b_drink = value;
          //emit stateChanged(value);
      }
      

      Pass the Config object on (from main.cpp) on. for example like this:

      //loop.h
      explicit Loop(Config *conf, QObject *parent = nullptr);
      ...
      private:
          Config *config = nullptr; 
      
      //loop.cpp
      Loop::Loop(Config *conf, QObject *parent) 
            : QObject(parent) , config(conf){
          flaskNo1 = new QTimer(this);
          flaskNo2 = new QTimer(this);
          flaskNo3 = new QTimer(this);
          flaskNo4 = new QTimer(this);
          flaskNo5 = new QTimer(this);
      
          connect(flaskNo1, SIGNAL(timeout()),this,SLOT(drinkNo1()));
          connect(flaskNo2, SIGNAL(timeout()),this,SLOT(drinkNo2()));
          connect(flaskNo3, SIGNAL(timeout()),this,SLOT(drinkNo3()));
          connect(flaskNo4, SIGNAL(timeout()),this,SLOT(drinkNo4()));
          connect(flaskNo5, SIGNAL(timeout()),this,SLOT(drinkNo5()));
      
          startLoop();
      }
      
      //main.cpp
      ...
          Config config;
          config.create();
      
          KeyTranslator ktr;
          Loop loop(&config);
      ...
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      4
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #2

        @BD9a said in Other Thread in my app dont get changed variable value:

        QQmlExpression: Expression qrc:/main.qml:104:17 depends on non-NOTIFYable properties:

        You need to add NOTIFY to your Q_PROPERTY and add a signal (and remeber to emit it when value changes!).

        (Z(:^

        B 1 Reply Last reply
        1
        • sierdzioS sierdzio

          @BD9a said in Other Thread in my app dont get changed variable value:

          QQmlExpression: Expression qrc:/main.qml:104:17 depends on non-NOTIFYable properties:

          You need to add NOTIFY to your Q_PROPERTY and add a signal (and remeber to emit it when value changes!).

          B Offline
          B Offline
          BD9a
          wrote on last edited by
          #3

          @sierdzio its necessary in this case? I have exacly same config in other project, and it work good

          sierdzioS 1 Reply Last reply
          0
          • B BD9a

            @sierdzio its necessary in this case? I have exacly same config in other project, and it work good

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            @BD9a said in Other Thread in my app dont get changed variable value:

            @sierdzio its necessary in this case? I have exacly same config in other project, and it work good

            Yes. Without NOTIFY, QML engine has no idea that value has updated.

            (Z(:^

            B 1 Reply Last reply
            1
            • sierdzioS sierdzio

              @BD9a said in Other Thread in my app dont get changed variable value:

              @sierdzio its necessary in this case? I have exacly same config in other project, and it work good

              Yes. Without NOTIFY, QML engine has no idea that value has updated.

              B Offline
              B Offline
              BD9a
              wrote on last edited by BD9a
              #5

              @sierdzio It could be useful, but I dont think it's my problem. In other project Q_PROPERTY without NOTIFY work's good, and probably one strong diffrence is that additional thread.

              b_drink variable is changing it's value, but function in other thread reading unchanged value.

              Removed this additional Thread (I dont really need this, those loops operate on ~3sec interval) and saw sth really weird. I changed this button to just print b_dring variable value (hardcoded as false) - in same class it prints false, in loop class it printing true wtf...

              J.HilkJ 1 Reply Last reply
              0
              • B BD9a

                @sierdzio It could be useful, but I dont think it's my problem. In other project Q_PROPERTY without NOTIFY work's good, and probably one strong diffrence is that additional thread.

                b_drink variable is changing it's value, but function in other thread reading unchanged value.

                Removed this additional Thread (I dont really need this, those loops operate on ~3sec interval) and saw sth really weird. I changed this button to just print b_dring variable value (hardcoded as false) - in same class it prints false, in loop class it printing true wtf...

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @BD9a minimal compileable example, please.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  BD9a
                  wrote on last edited by BD9a
                  #7

                  There is current code:
                  https://github.com/BD9a/test

                  Loop/loop.cpp printing b_drink defined in Config/config.h

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Are you sure your testapp does not crash? 'Config *config;' is unitialized...

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    B 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      Are you sure your testapp does not crash? 'Config *config;' is unitialized...

                      B Offline
                      B Offline
                      BD9a
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher It doesnt crash

                      Current value is:  false
                      true
                      true
                      true
                      
                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • B BD9a

                        @Christian-Ehrlicher It doesnt crash

                        Current value is:  false
                        true
                        true
                        true
                        
                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @BD9a Please fix your code, it can't work as I already explained.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        B 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @BD9a Please fix your code, it can't work as I already explained.

                          B Offline
                          B Offline
                          BD9a
                          wrote on last edited by
                          #11

                          @Christian-Ehrlicher how it should be done? In my opinion it should work (maybe cuz I dont understand it well?)

                          J.HilkJ 1 Reply Last reply
                          0
                          • B BD9a

                            @Christian-Ehrlicher how it should be done? In my opinion it should work (maybe cuz I dont understand it well?)

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #12

                            @BD9a I'm not entirely sure, what's going, or to be precise why everything doesn't go up in flames, but it is running on my pc and it shoudn't.

                            You can force this on your side as well by properly initializing the Config * inside Loop to a nullptr.

                            private:
                                Config *config = nullptr;
                            
                            

                            that will crash, and it should.

                            Here, what to do to fix this:

                            Enable writing & storing

                            void Config::w_drink(bool value)
                            {
                                qDebug() << "Current value is: " << b_drink;
                                if( b_drink == value)
                                     return;
                            
                                b_drink = value;
                                //emit stateChanged(value);
                            }
                            

                            Pass the Config object on (from main.cpp) on. for example like this:

                            //loop.h
                            explicit Loop(Config *conf, QObject *parent = nullptr);
                            ...
                            private:
                                Config *config = nullptr; 
                            
                            //loop.cpp
                            Loop::Loop(Config *conf, QObject *parent) 
                                  : QObject(parent) , config(conf){
                                flaskNo1 = new QTimer(this);
                                flaskNo2 = new QTimer(this);
                                flaskNo3 = new QTimer(this);
                                flaskNo4 = new QTimer(this);
                                flaskNo5 = new QTimer(this);
                            
                                connect(flaskNo1, SIGNAL(timeout()),this,SLOT(drinkNo1()));
                                connect(flaskNo2, SIGNAL(timeout()),this,SLOT(drinkNo2()));
                                connect(flaskNo3, SIGNAL(timeout()),this,SLOT(drinkNo3()));
                                connect(flaskNo4, SIGNAL(timeout()),this,SLOT(drinkNo4()));
                                connect(flaskNo5, SIGNAL(timeout()),this,SLOT(drinkNo5()));
                            
                                startLoop();
                            }
                            
                            //main.cpp
                            ...
                                Config config;
                                config.create();
                            
                                KeyTranslator ktr;
                                Loop loop(&config);
                            ...
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            4

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved