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
Forum Updated to NodeBB v4.3 + New Features

Referencing id from string

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 456 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 22 Jan 2024, 18:30 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?

    J 1 Reply Last reply 22 Jan 2024, 18:34
    0
    • P Paul Webster
      22 Jan 2024, 18:30

      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?

      J Offline
      J Offline
      JoeCFD
      wrote on 22 Jan 2024, 18:34 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 22 Jan 2024, 18:43
      0
      • J JoeCFD
        22 Jan 2024, 18:34

        @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 22 Jan 2024, 18:43 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 22 Jan 2024, 19:16
        0
        • P Paul Webster
          22 Jan 2024, 18:43

          @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 22 Jan 2024, 19:16 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 12 Feb 2024, 19:22

          1/4

          22 Jan 2024, 18:30

          • Login

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