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



  • On December 27, 2012 (yes, 2 years and more ago) DerekDomino's post was answered by Lukas Geyer by saying the folowing:

    " #include <QtGui>
    " #if QT_VERSION >= 0x050000
    " #include <QtWidgets>
    " #endif
    "
    "The recommendation however is to not use the module prefix anymore (#include <QApplication> "instead of #include <QtWidgets/QApplication>). It was never recommended to use module-wide "includes (#include <QtWidgets>)."

    I have two questions about this for this august assemblage of minds:

    1. Why does it work at all?
    2. Why does the code in the examples use module-wide includes?

    Thank you.


  • Moderators

    Hi, and welcome to the Qt Dev Net!

    Why? Simply because it was designed to work both ways in the beginning. However, people who wrote <QApplication> did not face issues when porting from Qt 4 to Qt 5, compared to people who wrote <QtGui/QApplication> or even <QtGui>.

    For simplicity, I guess. I use module-wide includes to quickly try something new. When my project becomes more mature, I replace the module-wide includes with individual-class includes. This reduces compilation times.



  • I see. Thank you, JKSH.

    I think this is still on topic. Can anyone explain why this does not work on 5.4.0 Windows MinGW? It doesn't seem to be defining all the Qt classes:

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

    Compiles fine on 4.8 Ubuntu, but receives errors indicative of missing class definitions on Windows. If I remove the conditional compile directives and simply use #include <QtWidgets> work fine on Windows. However, I don't find this conducive to maintaining a portable code base. Thanks.



  • Darn it, the editor widget played with my posting! The code snippet should have read (I hope this goes through all right:

    @
    #if QT_VERSION >= 0×050000
    #include <QtWidgets>
    #else
    #include <QtGui>
    #endif
    @

    [EDIT: Added '@' around code snippets -- JKSH]


  • Moderators

    I'm not sure. Have you double-checked that QT_VERSION >= 0×050000 is indeed returning true?

    What are the exact error messages you saw?

    Also, what's your rationale for supporting both Qt 4 and Qt 5?



  • Also in Qt5 you need to add
    @
    QT += widgets
    @
    to .pro file if you use qmake as a build tool.



  • @andreyc - I'm using "QT += core gui widgets" which QtCreator finds acceptable, and it compiles OK if I don't use the conditional in the source file.

    @JKSH - error: invalid use of incomplete type 'class QWidget'
    class BlahBlah : public QWidget
    ^
    I'm also deploying on Ubuntu, and Canonical only supports 4.8. I really don't want to blaze new trails, so I use what they supply.



  • Oh, and the #if is working. My evidence is that a #error inserted below the #include <QtWidgets> gets executed.



  • Well, here is the answer that I found:

    @#include <qglobal.h>
    #if QT_VERSION >= 0×050000
    #include <QtWidgets>
    #else
    #include <QtGui>
    #endif@


Log in to reply
 

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