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. Referencing id from string

Referencing id from string

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 812 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.
  • P Offline
    P Offline
    Paul Webster
    wrote on last edited by
    #1

    In my QML I have a grid of 64 of a custom component with id of a1 to h8. The custom component contains functions that I can call such as c2.clearPiece(). My C++ is returning the c2 to me as a string that I store into a var called from, however when I try to run the function from.clearPiece() I get "TypeError: Property 'clearPiece' of object c2 is not a function". Is there any way that I can use the data in the string to reference to the id? I have also set up objectName in the custom component. Is there any way that I can use a getChild() type function on the grid to get hold of the object?

    JoeCFDJ 1 Reply Last reply
    0
    • P Paul Webster

      In my QML I have a grid of 64 of a custom component with id of a1 to h8. The custom component contains functions that I can call such as c2.clearPiece(). My C++ is returning the c2 to me as a string that I store into a var called from, however when I try to run the function from.clearPiece() I get "TypeError: Property 'clearPiece' of object c2 is not a function". Is there any way that I can use the data in the string to reference to the id? I have also set up objectName in the custom component. Is there any way that I can use a getChild() type function on the grid to get hold of the object?

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      @Paul-Webster I guess you can find a child with objectName, but not with id.

      int main(int argc, char *argv[]) {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
          if (engine.rootObjects().isEmpty())
              return -1;
      
          // Assuming you have a QML root item
          QQuickItem* rootItem = qobject_cast<QQuickItem*>(engine.rootObjects().first());
      
          if (rootItem) {
              // Find child item by objectName
              QQuickItem* myObject = rootItem->findChild<QQuickItem*>("myObject");
      
              if (myObject) {
                  // Do something with myObject
                  qDebug() << "Found child object with objectName 'myObject'";
              } else {
                  qDebug() << "Child object with objectName 'myObject' not found";
              }
          }
      
          return app.exec();
      }
      
      P 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @Paul-Webster I guess you can find a child with objectName, but not with id.

        int main(int argc, char *argv[]) {
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        
            if (engine.rootObjects().isEmpty())
                return -1;
        
            // Assuming you have a QML root item
            QQuickItem* rootItem = qobject_cast<QQuickItem*>(engine.rootObjects().first());
        
            if (rootItem) {
                // Find child item by objectName
                QQuickItem* myObject = rootItem->findChild<QQuickItem*>("myObject");
        
                if (myObject) {
                    // Do something with myObject
                    qDebug() << "Found child object with objectName 'myObject'";
                } else {
                    qDebug() << "Child object with objectName 'myObject' not found";
                }
            }
        
            return app.exec();
        }
        
        P Offline
        P Offline
        Paul Webster
        wrote on last edited by
        #3

        @JoeCFD So I would need to access it through C++, I can't do it from within the QML?

        P 1 Reply Last reply
        0
        • P Paul Webster

          @JoeCFD So I would need to access it through C++, I can't do it from within the QML?

          P Offline
          P Offline
          Paul Webster
          wrote on last edited by
          #4

          @Paul-Webster I think that I have it:

          for(var i = 0; i < grid2.children.length; ++i)
          if(grid2.children[i].objectName === from){
          grid2.children[i].clearPiece()
          }

          1 Reply Last reply
          0
          • P Paul Webster has marked this topic as solved on

          • Login

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