December 27, 2012

DerekDomino DerekDomino
Lab Rat
28 posts

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:

  1. ... error: QtWidgets: No such file or directory

So, here is the command for changing the include’s

  1.  <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:

  1. -#include <QtGui>
  2. +#include <QtWidgets>
  3.  
  4. -#include <QtGui/QApplication>
  5. +#include <QApplication>

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

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

  1. -#include <QtGui/QApplication>
  2. +#include <QtWidgets/QApplication>
  3.  
  4. -#include <QtGui>
  5. +#include <QtWidgets>

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

Thanks,

4 replies

December 27, 2012

Zlatomir Zlatomir
Ant Farmer
367 posts

This works well for me:
in pro file:

  1. QT += core gui xml
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

And in code just include:
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QLabel>
  4. //...

December 27, 2012

DerekDomino DerekDomino
Lab Rat
28 posts

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

December 27, 2012

Zlatomir Zlatomir
Ant Farmer
367 posts

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 [qt-project.org] – 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.

December 31, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2068 posts

  1. #include <QtGui>
  2. #if QT_VERSION >= 0x050000
  3.     #include <QtWidgets>
  4. #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>).

 
  ‹‹ Qt 5: Unknown iconengines and jpeg modules      Problem opening a .pro file in Qt creator: Program stops responding ››

You must log in to post a reply. Not a member yet? Register here!