<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:
- Why does it work at all?
- Why does the code in the examples use module-wide includes?
Thank you.
-
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>
#endifCompiles 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.
-
-
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?
-
@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.