Solved Why can't this line be compiled? QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();
-
Using cmake in CLion.
//engine is QQmlApplicationEngine QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();
compile error:
error: invalid use of incomplete type ‘class QWindow’ 107 | uint win_width = w->width(); | ^~
No problem when using qtcreator and qmake before.
-
Hi,
Did you forget
#include <QWindow>
in that cpp file ?
-
Hi,
Did you forget
#include <QWindow>
in that cpp file ?
-
Yes, is indeed the problem, but why doesn't it say that QWindow cannot be found? Thanks.
-
@senmx said in Why can't this line be compiled? QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();:
but why doesn't it say that QWindow cannot be found?
Well, "incomplete type" means compiler does not know what it is, but it can't know in which header this type is defined, so it can't tell you which exact header you forgot to include.
-
"Incomplete type" means that
QWindow
is already declared as a type, but the full declaration of it cannot be found. Essentially, somewhere in the code (either your own or more likely another Qt header you already included) there is a line:class QWindow;
This tells the compiler that
QWindow
is a type. But it does not say what it looks like. For this you need the full declaration of the type which can be found in theQWindow
header file. So, the compiler is correct in saying "incomplete type" as it is known as type, but incomplete.BTW this is known as forward declaration in C++ and helps to reduce compile times when the full type does not have to be known everywhere it occurs. It can also help to reduce circular dependencies.