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. object->setProperty not working QML C++
QtWS25 Last Chance

object->setProperty not working QML C++

Scheduled Pinned Locked Moved General and Desktop
qmlc++qquickviewqqmlengine
2 Posts 2 Posters 3.0k 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.
  • A Offline
    A Offline
    AmalRaj
    wrote on last edited by p3c0
    #1

    Hi All

    I am trying to change QML loader visibie property from true to false from c++ code and its not working here is the code attached. please give me some suggestions.

    #Game.qml

    GameForm {
        Loader {
            id: scene1
            objectName: "scene1"
            source:"GameIntro.qml"
            visible: true
        }
        Loader {
            id: scene2
            objectName: "scene2"
            source:"GameBoard.qml"
            visible: false
        }
    }
    

    #Game.cpp

    QQmlEngine engine;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/Game.qml")));
    QObject *object = component.create();
    
    
    QObject *scene1 = object->findChild<QObject*>("scene1");
    if(scene1)
    {
            qDebug() << "Entered here";
            scene1->setProperty("visible", QString("false"));
           //scene1->setProperty("visible", false);
    }
    

    When i run this "Entered here" is coming. but the visible property not working.

    Note: i tested without passing as string too.

    Thanks for your help.
    Amal

    p3c0P 1 Reply Last reply
    0
    • A AmalRaj

      Hi All

      I am trying to change QML loader visibie property from true to false from c++ code and its not working here is the code attached. please give me some suggestions.

      #Game.qml

      GameForm {
          Loader {
              id: scene1
              objectName: "scene1"
              source:"GameIntro.qml"
              visible: true
          }
          Loader {
              id: scene2
              objectName: "scene2"
              source:"GameBoard.qml"
              visible: false
          }
      }
      

      #Game.cpp

      QQmlEngine engine;
      QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/Game.qml")));
      QObject *object = component.create();
      
      
      QObject *scene1 = object->findChild<QObject*>("scene1");
      if(scene1)
      {
              qDebug() << "Entered here";
              scene1->setProperty("visible", QString("false"));
             //scene1->setProperty("visible", false);
      }
      

      When i run this "Entered here" is coming. but the visible property not working.

      Note: i tested without passing as string too.

      Thanks for your help.
      Amal

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi @AmalRaj and Welcome,
      Whenever you create an item using QQmlComponent and create you must set it a visual parent using setParentItem. For that you will need to cast it to QQuickItem otherwise it wont be displayed

      QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/Game.qml")));
      QQuickItem *object = qobject_cast<QQuickItem*>(component.create());
      

      Then try setting its properties.

      P.S: The forum use markdown syntax. So while posting code surround it with ``` (3 backticks) so that it appears more readable. I have edited it this time.

      157

      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 Extensions
      • Unsolved