Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Qmenu::findChild is not working properly

    General and Desktop
    3
    5
    462
    Loading More Posts
    • 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.
    • Q
      Qt Enthusiast last edited by Qt Enthusiast

      Hi All
      The QMenu findChild is not working properly

      QMenu* lMenu;
      QMenu* existingMenu = lMenu->findChild<QMenu*>(QString("A..."));
      if (!existingMenu)
      lMenu->addMenu("A...");

      Can someone help me to solve the same . In this case existing menu is always NULL athat is why A... gets added multiple times

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Qt Enthusiast last edited by

        @Qt-Enthusiast said in Qmenu::findChild is not working properly:

        findChild

        the string you define in findchild specifies the objectname. Newly, by code not designer, created Objects have an empty string as ObjectName.

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 5
        • Q
          Qt Enthusiast last edited by

          could you please code for me

          jsulm 1 Reply Last reply Reply Quote 0
          • J.Hilk
            J.Hilk Moderators last edited by

            untested.

            QMenu* lMenu;
            QMenu* existingMenu = lMenu->findChild<QMenu*>(QString("A..."));
            if (!existingMenu){
                 existingMenu  = new  QMenu("A...");
                 existingMenu->setObjectName("A...");
                 lMenu->addMenu(existingMenu);
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

            Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply Reply Quote 1
            • jsulm
              jsulm Lifetime Qt Champion @Qt Enthusiast last edited by

              @Qt-Enthusiast said in Qmenu::findChild is not working properly:

              could you please code for me

              Could you please read documentation: http://doc.qt.io/qt-5/qobject.html#objectName-prop ?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 2
              • First post
                Last post