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. <Solved> Qt 5 and Qt 4 compatibility (#include <QtWidgets>) (reprise)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.2k 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.
  • N Offline
    N Offline
    norisezp
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • N Offline
        N Offline
        norisezp
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          norisezp
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            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?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • N Offline
                N Offline
                norisezp
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  norisezp
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    norisezp
                    wrote on last edited by
                    #9

                    Well, here is the answer that I found:

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

                    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