Ricardo Melo Ricardo Melo - 10 months ago 42
C++ Question

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()){
main_window = window;
return main_window;

is class of my project and
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.


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