Ricardo Melo
Ricardo Melo

Reputation: 25

How to retrieve a QWidget from a list of opened windows

In some part of my project, I want to get the reference of a window, from a list of opened windows. So, i'm doing this way:

QWidget* WindowUtil::mainWindow() {
    QWidget* main_window = nullptr;
    for(QWidget *window: QApplication::allWidgets()){
        if(QString(window>metaObject()->className()).contains("Home")){
            main_window = window;
            break;
        }
    }
    return main_window;
}

WindowUtil is class of my project and mainWindow() is a static method.

However, this solution doesn't work. The compiler says:

error: incomplete type 'QApplication' used in nested name specifier
 for(QWidget *window : QApplication::allWidgets()){
                       ^

And I'm stuck here.

Upvotes: 2

Views: 60

Answers (1)

mtszkw
mtszkw

Reputation: 2783

Even though the answer has been already posted as a comment, this error should mean (most of the time) that you have not included right header file, in this case: <QApplication> and your app cannot find the declarations for what it wants.

#include <QApplication>

instruction in the file that raises an error should be an efficient solution for your problem :)

Upvotes: 2

Related Questions