Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML property is not changing from inside the same file
Forum Updated to NodeBB v4.3 + New Features

QML property is not changing from inside the same file

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 989 Views 2 Watching
  • 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.
  • K Offline
    K Offline
    krokstr
    wrote on 10 Aug 2018, 13:20 last edited by
    #1

    Hello,

    I'm having an issue with reading the correct value of a QML property from C++.

    main.qml

    ApplicationWindow {
        id: applicationWindow
        visible: true
        width: 640
        height: 480
        title: qsTr("Tabs")
    
        property int valuef1: page1.valuef33
    .
    .
    .
           Page1{
                id:page1
    
            }
    .
    .
    .
    }
    

    "other_files_reaching_each_other's_propeperty_the same_way".qml

    Page1Form {
    
        property int valuef33: freqWindow.valuef44
    
    }
    

    ButtonLine.qml

    ButtonLineForm {
    
     property int  valuef55: 558
    
        mArea{        
            onClicked: {
                console.log("test:", valuef55)
            }
        }
        mArea1{
            onClicked: {
                valuef55 = 4
            }
        }
        mArea2{
            onClicked: {
                valuef55 = 3
            }
        }
    .
    .
    .
    }
    

    In C++ I'm reaching the valuef1 property of main.qml:

    qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
    

    All the properties are connected, so I'm reading "Property value: 558". But when I click on the mArea1, and then try to read the property value, which should be equal to 4 after the click, it shows 558 again. Clicking mArea after mArea1 and printing valuef55 in the console shows it's equal to 4.

    What should I do to get the right value in C++? Is this the correct way of getting a value from QML item, and if it's not, how to do it in better way?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 10 Aug 2018, 13:57 last edited by
      #2

      Are you getting any error ? What is the value are you getting ?
      object what you passed to QQmlProperty is it the correct object where property exist in qml ?
      Now why do you want to access the value in C++ ? Do you want to pass the value some where ?

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • K Offline
        K Offline
        krokstr
        wrote on 10 Aug 2018, 14:06 last edited by
        #3

        I'm not getting any error, I'm getting wrong value. I have 5 buttons which pressed should correspondingly change the property value of "valuef55" in ButtonLine.qml. When I define "valuef55" I'm giving value "558" , and that's what I'm reading from C++, regardless what button has been pressed. The idea is to read from C++ which one of the 5 buttons has been pressed.

        D 1 Reply Last reply 10 Aug 2018, 16:52
        0
        • K krokstr
          10 Aug 2018, 14:06

          I'm not getting any error, I'm getting wrong value. I have 5 buttons which pressed should correspondingly change the property value of "valuef55" in ButtonLine.qml. When I define "valuef55" I'm giving value "558" , and that's what I'm reading from C++, regardless what button has been pressed. The idea is to read from C++ which one of the 5 buttons has been pressed.

          D Offline
          D Offline
          Diracsbracket
          wrote on 10 Aug 2018, 16:52 last edited by Diracsbracket 8 Oct 2018, 16:55
          #4

          @krokstr said in QML property is not changing from inside the same file:

          qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();

          Since your question is about getting the value property valuef1 of applicationWindow from C++, and since that value never seems to change, have you checked that it effectively changes in QML when you press your buttons in QML ?

          e.g. does this work:

          applicationWindow {
               ...
               property int valuef1: page1.valuef33
          
               onValuef1Changed : {
                    console.debug("valuef1 changed: " + valuef1)
               }
          }
          
          

          Since object seems to point to an instance of ApplicationWindow , can you show how you obtain that object pointer to it?

          Furthermore, can you show us where in your C++ code you try to use this debug statement:

          qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
          
          1 Reply Last reply
          0
          • K Offline
            K Offline
            krokstr
            wrote on 13 Aug 2018, 07:56 last edited by
            #5

            Okay, I've just tried that the "valuef1" changes all the way to "applicationWindow", so the problem is in my C++ code.

            main.cpp

            int main(int argc, char *argv[])
            {
                QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
            
                QGuiApplication app(argc, argv);
            
                QQmlApplicationEngine engine;
            
            
            
                my_class My_class;
                engine.rootContext()->setContextProperty("My_class", &My_class);
                engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            
                if (engine.rootObjects().isEmpty())
                    return -1;
            
                return app.exec();
            }
            

            my_class.cpp

            my_class::my_class(QObject *parent) : QObject(parent)
            {
                object = NULL;
                engine = new QQmlEngine;
                component = new QQmlComponent(engine, "qrc:/main.qml");
                object = component->create();
            
            
            }
            my_class::~my_class()
            {
                if(object)
                {
                    delete object;
                    object = NULL;
                }
                if(engine)
                {
                    delete engine;
                    engine = NULL;
                }
                if(component)
                {
                    delete component;
                    component = NULL;
                }
            }
            
            void my_class::connection_test()
            {
                qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
            }
            

            I know that's not the correct way of doing it, but I've tried many examples and haven't managed it to run. So I'm just giving you the last one I've left in my code.

            connection_test() is a slot which is called when a button in QML is pressed.

                mouseArea{
                    onClicked: My_class.connection_test()
                }
            

            With this configuration, when I call "connection_test()", it is giving me an error "ReferenceError: My_class is not defined". So I believe I'm doing a deadlock, but I don't know how to fix it. In many of my tries, it started printing the qDebug in "connection_test()", but after that I had to push the back button of my device to continue, otherwise the app freezes.

            D 1 Reply Last reply 13 Aug 2018, 12:48
            0
            • K krokstr
              13 Aug 2018, 07:56

              Okay, I've just tried that the "valuef1" changes all the way to "applicationWindow", so the problem is in my C++ code.

              main.cpp

              int main(int argc, char *argv[])
              {
                  QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
              
                  QGuiApplication app(argc, argv);
              
                  QQmlApplicationEngine engine;
              
              
              
                  my_class My_class;
                  engine.rootContext()->setContextProperty("My_class", &My_class);
                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
                  if (engine.rootObjects().isEmpty())
                      return -1;
              
                  return app.exec();
              }
              

              my_class.cpp

              my_class::my_class(QObject *parent) : QObject(parent)
              {
                  object = NULL;
                  engine = new QQmlEngine;
                  component = new QQmlComponent(engine, "qrc:/main.qml");
                  object = component->create();
              
              
              }
              my_class::~my_class()
              {
                  if(object)
                  {
                      delete object;
                      object = NULL;
                  }
                  if(engine)
                  {
                      delete engine;
                      engine = NULL;
                  }
                  if(component)
                  {
                      delete component;
                      component = NULL;
                  }
              }
              
              void my_class::connection_test()
              {
                  qDebug() << "Property value:" << QQmlProperty::read(object, "valuef1").toInt();
              }
              

              I know that's not the correct way of doing it, but I've tried many examples and haven't managed it to run. So I'm just giving you the last one I've left in my code.

              connection_test() is a slot which is called when a button in QML is pressed.

                  mouseArea{
                      onClicked: My_class.connection_test()
                  }
              

              With this configuration, when I call "connection_test()", it is giving me an error "ReferenceError: My_class is not defined". So I believe I'm doing a deadlock, but I don't know how to fix it. In many of my tries, it started printing the qDebug in "connection_test()", but after that I had to push the back button of my device to continue, otherwise the app freezes.

              D Offline
              D Offline
              Diracsbracket
              wrote on 13 Aug 2018, 12:48 last edited by Diracsbracket
              #6

              Hi @krokstr
              Have a look at this:
              https://stackoverflow.com/questions/23177839/how-can-i-access-my-window-object-properties-from-c-while-using-qqmlapplicatio
              It clearly shows what you need to do to access your main QML window object.

              1 Reply Last reply
              1
              • K Offline
                K Offline
                krokstr
                wrote on 14 Aug 2018, 08:14 last edited by
                #7

                Thank you, that link was the key! I made it!

                1 Reply Last reply
                1

                1/7

                10 Aug 2018, 13:20

                • Login

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