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 find a widget through QApplication::instance()?
Forum Updated to NodeBB v4.3 + New Features

How to find a widget through QApplication::instance()?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.1k 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.
  • M Offline
    M Offline
    MT339M
    wrote on last edited by MT339M
    #1
    Main_window::Main_window (QWidget *parent) : QMainWindow(parent) {
        QHBoxLayout *layout = new QHBoxLayout;
        QPushButton *button = new QPushButton("button");
        QPushButton *button2 = new QPushButton("button2");
    
        button->connect(button, &QPushButton::clicked, this, &Main_window::do_something);
        button2->setObjectName("button2");
    
        layout->addWidget(button);
        layout->addWidget(button2);
    
        this->UI.setupUi(this);
        this->centralWidget()->setLayout(layout);
    }
    void Main_window::do_something ()  {
        QPushButton *button2 = QApplication::instance()->findChild<QPushButton *>("button2");
        
        qDebug() << button2; // QWidget(0x0)
    }
    

    I want to find a widget using findChild() method with QApplication::instance(), but it's not working.

    QApplication::instance()->findChild<QPushButton *>("button2");
    

    When I use findChild() method with Main_window's instance, it works well.

    this->findChild<QPushButton *>("button2");
    

    Could you tell me why it fails with QApplication::instance(), and how to solve this? Thanks in advance.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't know exactly why it does not work but also don't understand why you would need it.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MT339M
        wrote on last edited by MT339M
        #3

        @Christian-Ehrlicher
        I want to access a widget from afar without pass widget as argument.

        void Main_window::do_something ()  {
            for (auto &widget : QApplication::allWidgets())
                if (widget->objectName() == "button2") {
                    QPushButton *button2 = static_cast<QPushButton *>(widget);
                    
                    qDebug() << button2;
                }
        }
        

        The above solution is working well. It's weird..

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          Looks like no good design.
          Please provide a minimal, compileable testcase.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            Not a good design +1.
            However, about the question, it's simple, because a widget (or any other QObject) is normally not QApplication::instance()'s child, unless you have set the parent to be it.
            You could use QApplication::allWidgets() or QApplication::topLevelWidgets() to iterate widgets of the application, but still not a good design.

            M 1 Reply Last reply
            3
            • B Bonnie

              Not a good design +1.
              However, about the question, it's simple, because a widget (or any other QObject) is normally not QApplication::instance()'s child, unless you have set the parent to be it.
              You could use QApplication::allWidgets() or QApplication::topLevelWidgets() to iterate widgets of the application, but still not a good design.

              M Offline
              M Offline
              MT339M
              wrote on last edited by
              #6

              @Bonnie
              Thanks for your answer!

              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