Qt 5 and Qt 4 compatibility (#include <QtWidgets>)



  • Hello,

    I've used the fixqt4headers.pl script to make compatible the include's lines between Qt 4 and 5. However, when compiling the modified source code against Qt 4.8.1, the <QtWidgets> header file is not found:
    @
    ... error: QtWidgets: No such file or directory
    @

    So, here is the command for changing the include's
    @
    <myPath>/Qt5.0.0/5.0.0/Src/qtbase/bin/fixqt4headers.pl --qtdir <myPath>/Qt5.0.0/5.0.0/clang_64 --verbose --strip-modules
    @

    How it changed the source code:
    @
    -#include <QtGui>
    +#include <QtWidgets>

    -#include <QtGui/QApplication>
    +#include <QApplication>
    @

    Note that even without stripping modules prefix (no --strip-modules option for fixqt4headers.pl), header files are not found:

    @
    ...error: QtWidgets/QApplication: No such file or directory
    @

    @
    -#include <QtGui/QApplication>
    +#include <QtWidgets/QApplication>

    -#include <QtGui>
    +#include <QtWidgets>
    @

    Do you know what to do to be able to compile both with Qt 4.8 and Qt 5?

    Thanks,



  • This works well for me:
    in pro file:
    @QT += core gui xml
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets@
    And in code just include:
    @#include <QApplication>
    #include <QWidget>
    #include <QLabel>
    //...
    @



  • Thanks Ziatomir, but what do you use for <QtWidgets> that is the Qt5 replacement of <QtGui> v4?



  • It's not really recommended, but you can use: #include <QtWidgets>, anyway it's better if you include the headers only for the widgets you use. //by better i mean that the compilation time might be better and also someone had problems with auto-complete in Creator when the whole module was included

    QtWidgets module it's not actually a replacement for QtGui in Qt 4, read more about Qt 5 modules "here":https://qt-project.org/doc/qt-5.0/qtdoc/modules.html - as i know QtGui has base classes GUI components and OpenGl and Widgets module is a part that contains the QWidget and classes derived from QWidget... anyway read more in the documentation for better description of modules.



  • @
    #include <QtGui>
    #if QT_VERSION >= 0x050000
    #include <QtWidgets>
    #endif
    @

    The recommendation however is to not use the module prefix anymore (<code>#include <QApplication></code> instead of <code>#include <QtWidgets/QApplication></code>). It was never recommended to use module-wide includes (<code>#include <QtWidgets></code>).


Log in to reply
 

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