Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Qt Creator not recognising Widgets despite inlcuding all necessary includes
DeanQt last edited by
Qt creator doesnt recognise QTreeWidget when typed in. I literally included all header files:
And even the tutorial Im watching had LESS exhaustive header files and worked for them (they missed the QWidget include)
I do understand that:
is one way of making sure the compiler includes the TreeWidgetItem, which does work if I include it, but I dont see why that needs to be in there; I included QWidget which includes ALL widgets.
Infact in the tutorial the only includes were @#include <QDialog>
i.e. it didnt even include QWidget and the tutorial pulled it off!
Please can anyone explain why this is so???
arnolddumas last edited by
The thing is the tutorial you're reading was written for Qt4. In Qt5, some includes must be changed.
In Qt4 the <QtGui> include was used to make all the widgets known by the compiler. But in Qt5, the widgets (QLineEdit, QTextEdit ....) have been moved to a brand new module called 'widgets'. Therefore you'll need to replace QtGui by QtWidgets
Notice there's a 's' at the end of the QtWidgets include.
So your includes should now look like that :
Also notice that QDialog is already included with QtWidgets.
And don't forget to add the new 'widgets' module in your *.pro file.
Now everything should just works fine.
ChrisW67 last edited by
bq. I included QWidget which includes ALL widgets
Including <QWidget> gives the compiler a complete declaration of the QWidget base class. It does not generally include a declaration of sub-classes like QLabel, QTextEdit etc.; they have their own includes.
Including <QtGui> (Qt4) or <QtWidgets> (Qt5) does include complete declarations for all widgets. You should not get into the habit of including all of the GUI includes for non-trivial project unless you are particularly enamoured of longer-than-necessary build times.
It's not clear what you mean by, "Qt creator doesnt recognise QTreeWidget when typed in." Qt Creator cannot perform auto-complete functions for classes without a complete declaration of a class. If you only have a forward declaration, perhaps provided by a related include that doesn't itself require a full declaration, then Creator knows only that the class exists but not how it is built so it cannot offer completion for member functions etc. For example, the QWidget include declares the existence but not the content of class QLayout, QVariant, QStyle etc.
DeanQt last edited by
Thanks guys, I included QtWidgets and it seems to work!