Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot modify Qml propriety from Cpp
Qt 6.11 is out! See what's new in the release blog

Cannot modify Qml propriety from Cpp

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 7.7k Views 1 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    I guess you need to set that property after even loop is started in app.exec().

    Otherwise, the code seems to be correct (although there is no need to modify buttonOpenCloseCom, when you can just modify buttonComText in ApplicationWindow).

    (Z(:^

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #3

      Hi, thanks for the answer.
      I've changed the code:
      @int main(int argc, char *argv[])
      {
      QGuiApplication app(argc, argv); //preparo l'applicazione
      QQmlApplicationEngine engine(QUrl("qml/main.qml")); //carico l'interfaccia
      QObject *radice = engine.rootObjects().value(0); //identifico la radice
      QQuickWindow *applicationwindow = qobject_cast<QQuickWindow *>(radice); //acquisisco in C++ l'ogg QML ApplicationWindow
      if ( !applicationwindow )
      {
      qWarning("Error: Your root item has to be a Window.");
      return -1;
      }
      applicationwindow->show();
      return app.exec();

      QObject *button = radice->findChild<QObject*>("buttonOpenCloseCom",Qt::FindChildrenRecursively);
      if (button)
      {
          button->setProperty("text", "Open");
          qDebug()<<"found!";
      }
      else
      {
          button->setProperty("text", "Open");
          qDebug()<<"not found!";
      }
      

      }@

      but there is no output.
      The problem persists!

      I've also tried to change the buttonComText in ApplicationWindows, but the result is the same.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wspilot
        wrote on last edited by
        #4

        Try this:
        objectName: "buttonOpenCloseCom"

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #5

          There is yet the objectName proprety.
          I've tried this solution... but doesn't work.
          [quote author="wspilot" date="1376930837"]Try this:
          objectName: "buttonOpenCloseCom"[/quote]

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wspilot
            wrote on last edited by
            #6

            In your code it is still an identifier/var or whatever, but not a string
            objectName: buttonOpenCloseCom
            not
            objectName: “buttonOpenCloseCom”

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              Excuseme but in the code:
              @
              Button
              {
              id: buttonOpenCloseCom
              objectName: buttonOpenCloseCom
              text:applicationwindow.buttonComText
              enabled: true
              }
              @

              objectName: “buttonOpenCloseCom” there is![/quote]

              what is the problem you talk about?!

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #8

                Put it in quotes: ". Not just string but "string".

                (Z(:^

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wspilot
                  wrote on last edited by
                  #9

                  @ Button
                  {
                  id: buttonOpenCloseCom
                  objectName: "buttonOpenCloseCom"
                  text:applicationwindow.buttonComText
                  enabled: true
                  }@

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #10

                    I've understand what you mean, and I've tried with the correction, but the result is the same as before.
                    Debugging i've see that the root object is correctly the "applicationwindow",
                    but when I try to find the child, it fails and return a null pointer...
                    in "this":http://qt-project.org/doc/qt-5.1/qtqml/qtqml-cppintegration-interactqmlfromcpp.html#loading-qml-objects-from-c reference it used QQuickView, and not QQuickWindow to handle the root object...
                    Could it be a problem?
                    If yes, how can I resolve it? any suggestions?

                    1 Reply Last reply
                    0
                    • mranger90M Offline
                      mranger90M Offline
                      mranger90
                      wrote on last edited by
                      #11

                      The problem is that you make the change after the return() statement.
                      Nothing after that will be executed.

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #12

                        I've modified the code:
                        @int main(int argc, char *argv[])
                        {
                        QGuiApplication app(argc, argv); //preparo l'applicazione
                        QQmlApplicationEngine engine(QUrl("qml/main.qml")); //carico l'interfaccia
                        QObject *radice = engine.rootObjects().value(0); //identifico la radice
                        QQuickWindow *applicationwindow = qobject_cast<QQuickWindow *>(radice); //acquisisco in C++ l'ogg QML ApplicationWindow
                        if ( !applicationwindow )
                        {
                        qWarning("Error: Your root item has to be a Window.");
                        return -1;
                        }
                        QObject button = radice->findChild<QObject>("buttonOpenCloseCom",Qt::FindChildrenRecursively);
                        if (button)
                        {
                        button->setProperty("text", "Open");
                        qDebug()<<"found!";
                        }
                        else
                        {
                        button->setProperty("text", "Open");
                        qDebug()<<"not found!";
                        }

                        applicationwindow->show();
                        return app.exec();
                        

                        }@

                        but is the same as before...
                        [quote author="mranger90" date="1376947874"]The problem is that you make the change after the return() statement.
                        Nothing after that will be executed.[/quote]

                        1 Reply Last reply
                        0
                        • sierdzioS Offline
                          sierdzioS Offline
                          sierdzio
                          Moderators
                          wrote on last edited by
                          #13

                          One drawback of QML is that it's mightily hard to explain certain things... In order for this to work (IMO) you need to do that property manipulation outside of "main.cpp":https://github.com/sierdzio/closecombatfree/blob/master/src/ccfmain.cpp so for example in a QObject that gets initialised somewhere later. Same as with QWidgets or console apps in Qt: you need to have a working event loop for moc to work.

                          One possible alternative would be to modify the property using Qt::QueuedConnection, or the infamous singleshot QTimer.

                          (Z(:^

                          1 Reply Last reply
                          0

                          • Login

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