How to retrieve a QWidget from a list of opened windows

c++ qt c++11

42 просмотра

1 ответ

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.

Автор: Ricardo Melo Источник Размещён: 08.11.2019 11:22

Ответы (1)


2 плюса

Решение

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 :)

Автор: Mateusz Размещён: 20.08.2016 02:09
Вопросы из категории :
32x32