Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 5 and Qt 4 compatibility (#include <QtWidgets>)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 41.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DerekDomino
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      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>
      //...
      @

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DerekDomino
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • ZlatomirZ Offline
          ZlatomirZ Offline
          Zlatomir
          wrote on last edited by
          #4

          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.

          https://forum.qt.io/category/41/romanian

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            @
            #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>).

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved