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. How to get a pointer to a specific widget when you know only its name?
Forum Updated to NodeBB v4.3 + New Features

How to get a pointer to a specific widget when you know only its name?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 741 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.
  • Y Offline
    Y Offline
    Ylvy
    wrote on 17 Apr 2023, 12:09 last edited by
    #1

    I tried below but its returning null.

    QMainWindow* window = QApplication::instance()->findChild<QMainWindow*>("AppClass");
    

    QWidget* mainWindow = qApp->activeWindow(); work only if the window is active, would not help.

    J 1 Reply Last reply 17 Apr 2023, 12:27
    0
    • Y Ylvy
      17 Apr 2023, 12:45

      @J-Hilk Yes it does, so theres no other option than iterating the topLevelWidgets? im trying to get a pointer to the QMainWindow exclusively to get a pointer to a specific widget.

      J Online
      J Online
      JonB
      wrote on 17 Apr 2023, 12:52 last edited by JonB
      #8

      @Ylvy
      Yes, if you want to find a top-level window, assuming your QMainWindow is such, and you are somewhere that does not have access to your QMainWindow* variable, walk (don't descend) QWidgetList QApplication::topLevelWidgets() for a QMainWindow*. Then use that for mainWindow->findChild<YourWidgetClass*>("SpecificWidgetObjectName");

      I use this for debugging purposes. It may not be a sign of great design if your code really needs to find the main window for this purpose.

      1 Reply Last reply
      3
      • J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 17 Apr 2023, 12:20 last edited by
        #2

        @Ylvy Why do you need to get the pointer to your main window using the name? Main window does not necessarily have a parent.
        Usually you call findChild from the parent (or anywhere higher in the hierarchy) of the widget.

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

        Y 1 Reply Last reply 17 Apr 2023, 12:23
        0
        • J jsulm
          17 Apr 2023, 12:20

          @Ylvy Why do you need to get the pointer to your main window using the name? Main window does not necessarily have a parent.
          Usually you call findChild from the parent (or anywhere higher in the hierarchy) of the widget.

          Y Offline
          Y Offline
          Ylvy
          wrote on 17 Apr 2023, 12:23 last edited by Ylvy
          #3

          @jsulm i'm trying to get a child from the QMainWindow, when i know only its name, but i'm trying to avoid passing/saving a pointer to the widget/QMainWindow.

          J 1 Reply Last reply 17 Apr 2023, 12:25
          0
          • Y Ylvy
            17 Apr 2023, 12:23

            @jsulm i'm trying to get a child from the QMainWindow, when i know only its name, but i'm trying to avoid passing/saving a pointer to the widget/QMainWindow.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 17 Apr 2023, 12:25 last edited by
            #4

            @Ylvy said in How to get a pointer to a specific widget when you know only its name?:

            i'm trying to get a child from the QMainWindow

            No, you don't.
            You're trying to get pointer to QMainWindow instance using its name...

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

            1 Reply Last reply
            1
            • Y Ylvy
              17 Apr 2023, 12:09

              I tried below but its returning null.

              QMainWindow* window = QApplication::instance()->findChild<QMainWindow*>("AppClass");
              

              QWidget* mainWindow = qApp->activeWindow(); work only if the window is active, would not help.

              J Online
              J Online
              JonB
              wrote on 17 Apr 2023, 12:27 last edited by JonB
              #5

              @Ylvy
              As @jsulm has said. You are looking for a QMainWindow* named AppClass.

              Main window does not necessarily have a parent.

              An alternative call you can use is QWidgetList QApplication::topLevelWidgets() to examine or descend (findChild()) from.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 17 Apr 2023, 12:42 last edited by
                #6

                In addition to what the others said, are you sure, your instance actually has an objectName to look for ? A default object name is only done via QDesigner - plugin.


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


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

                Y 1 Reply Last reply 17 Apr 2023, 12:45
                0
                • J J.Hilk
                  17 Apr 2023, 12:42

                  In addition to what the others said, are you sure, your instance actually has an objectName to look for ? A default object name is only done via QDesigner - plugin.

                  Y Offline
                  Y Offline
                  Ylvy
                  wrote on 17 Apr 2023, 12:45 last edited by
                  #7

                  @J-Hilk Yes it does, so theres no other option than iterating the topLevelWidgets? im trying to get a pointer to the QMainWindow exclusively to get a pointer to a specific widget.

                  J 1 Reply Last reply 17 Apr 2023, 12:52
                  0
                  • Y Ylvy
                    17 Apr 2023, 12:45

                    @J-Hilk Yes it does, so theres no other option than iterating the topLevelWidgets? im trying to get a pointer to the QMainWindow exclusively to get a pointer to a specific widget.

                    J Online
                    J Online
                    JonB
                    wrote on 17 Apr 2023, 12:52 last edited by JonB
                    #8

                    @Ylvy
                    Yes, if you want to find a top-level window, assuming your QMainWindow is such, and you are somewhere that does not have access to your QMainWindow* variable, walk (don't descend) QWidgetList QApplication::topLevelWidgets() for a QMainWindow*. Then use that for mainWindow->findChild<YourWidgetClass*>("SpecificWidgetObjectName");

                    I use this for debugging purposes. It may not be a sign of great design if your code really needs to find the main window for this purpose.

                    1 Reply Last reply
                    3
                    • Y Ylvy has marked this topic as solved on 17 Apr 2023, 13:19

                    1/8

                    17 Apr 2023, 12:09

                    • Login

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