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. Enumerate Children Push Buttons from a Window Handle

Enumerate Children Push Buttons from a Window Handle

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • C Offline
    C Offline
    Chris31
    wrote on last edited by
    #1

    Hello,

    I would like to enumerate the push buttons from a main window handle.

    In the sample below (Win32 api FindWindow() to get the main window handle, just for testing but I will have to use it in the final application), the foreach loop gives : 1 QRubberBand instead of the 2 QPushButtons.
    Why ?

    Thanks.

    @
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QHBoxLayout>

    #include <Windows.h>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QMainWindow *window = new QMainWindow();
    window->setWindowTitle(QString::fromUtf8("Test QPushButtons"));
    window->resize(500,300);

    QPushButton* button1 = new QPushButton("Button 1");
    QPushButton* button2 = new QPushButton("Button 2");

    button1->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    button2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    QWidget* centralWidget = new QWidget(window);
    centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    QHBoxLayout* layout = new QHBoxLayout(centralWidget);
    layout->addWidget(button1);
    layout->addWidget(button2);

    window->setCentralWidget(centralWidget);
    window->show();

    HWND hWnd = FindWindow(NULL, L"Test QPushButtons");

    if (hWnd)
    {
    QWidget* widget = QWidget::find((WId)hWnd);

    QList<QWidget*> widgets = widget->findChildren<QWidget*>();
    foreach (QWidget *widget, widgets)
    {
    widget->setStyleSheet("background-color:black;");
    }
    }

    return app.exec();
    }
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ckakman
      wrote on last edited by
      #2

      Hi,

      I don't know why you need FindWindow. You can get your buttons with:
      @
      window->findChildren<QPushButton*>();
      @

      findChild() and findChildren() are QObject's members. So any QObject-derived class can look for its children.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris31
        wrote on last edited by
        #3

        window->findChildren is same as widget->findChildren,
        but I saw it is the foreach loop which doesn't work, because I can see by debugging all the windows in widgets (QRubberBand, QObject and the 2 QPushButtons)

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on last edited by
          #4

          foreach works just fine. If you ask for all widgets, you get all widgets.

          You are looking at the wrong place in may one line of sample code. Note that I have specified which type of children I am looking for (hint: look between < and >):
          @
          window->findChildren<QPushButton*>();

          or

          widget->findChildren<QPushButton*>();
          @

          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