Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include <QtWidgets> not working in Qt 4

Tags:

qt

qt5

A developer added a feature to our software using QtWidgets which as far as I can tell is a QT5 feature. The problem is that our software is still designed for QT4 and we cannot move to the latest version of QT just yet. The new feature works great when compiled in QT5 but when trying to compile using QT 4.8.5 brings up the following error.

src\qt\snapwidget.h:3: error: QtWidgets: No such file or directory
#include <QtWidgets>
                   ^

Is there any documentation on back porting QtWidgets to older version of QT? Perhaps someone was cooking up something like widgets before it made its way in as a native feature.

like image 767
Peter Bushnell Avatar asked Dec 06 '22 01:12

Peter Bushnell


1 Answers

Widgets are not a Qt 5 feature. They are in Qt since day 1.


In general, I advise against using such per-module include. They will add all includes for the given module, raising your compilation times dramatically. Instead you can use

#include <QClass>

which will work just as well, and require only the adjustment to the .pro file I said below (one place, instead of modifying all source files).


Your problem is that there's no "QtWidgets" module in Qt 4. Widgets in Qt 4 were in the "gui" module.

Hence, the "equivalent" line in Qt 4 is

#include <QtGui>

You can use QT_VERSION to make your C++ code work in both Qt 4 and 5:

#include <QtGlobal>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#  include <QtWidgets>
#else
#  include <QtGui>
#endif

Add a similar logic in the .pro file for linking against the widgets module. Both Qt 4 and Qt 5 by default link only the core and the gui modules, but as I've just said, that doesn't bring you widgets in Qt 5:

greaterThan(QT_MAJOR_VERSION, 4):QT += widgets
like image 155
peppe Avatar answered Jan 18 '23 22:01

peppe