[SOLVED] Qt Creator not recognising Widgets despite inlcuding all necessary includes



  • Qt creator doesnt recognise QTreeWidget when typed in. I literally included all header files:
    @#include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include <QWidget>@

    And even the tutorial Im watching had LESS exhaustive header files and worked for them (they missed the QWidget include)

    I do understand that:
    @#include <QTreeWidgetItem>@
    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>
    #include <QtCore>
    #include <QtGui>@
    i.e. it didnt even include QWidget and the tutorial pulled it off!

    Please can anyone explain why this is so???



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

    @#include <QtCore>
    #include <QtWidgets>
    @

    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.



  • 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.



  • Thanks guys, I included QtWidgets and it seems to work!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.