Results 1 to 4 of 4

Thread: compiling under qt4 AND qt5

  1. #1
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default compiling under qt4 AND qt5

    In Qt 4.x, one typically needs to #include <QtGui>, and in Qt 5.x much of the stuff there is moved to <QtWidgets>

    What are the recommended ifdefs needed in c++ source code and .pro -files in order to have a project compile under both Qt4 and Qt5 ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: compiling under qt4 AND qt5

    Quote Originally Posted by drhex View Post
    In Qt 4.x, one typically needs to #include <QtGui>, and in Qt 5.x much of the stuff there is moved to <QtWidgets>
    Actually you should never use the two above because this significatly increases compile time. You should always include just the classes you need

    What are the recommended ifdefs needed in c++ source code and .pro -files in order to have a project compile under both Qt4 and Qt5 ?
    Qt Code:
    1. #if QT_VERSION >= 0x050000
    2. // Qt5 code
    3. #else
    4. // Qt4 code
    5. #endif
    To copy to clipboard, switch view to plain text mode 

    qmake Code:
    1. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following 2 users say thank you to wysota for this useful post:

    drhex (15th March 2013), nils_heidorn (25th November 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: compiling under qt4 AND qt5

    It appears one needs to
    Qt Code:
    1. #include <qglobal.h>
    To copy to clipboard, switch view to plain text mode 

    first, or else QT_VERSION won't be defined.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: compiling under qt4 AND qt5

    If you take wysota's advice about individual class #includes there will usually be no reason to #ifdef between Qt4 and Qt5 in your code. Even if there is a need for version based code the first #include <QApplication>, <QWidget> etc. will have brought qglobal.h in anyway.

    This:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QTableView>
    To copy to clipboard, switch view to plain text mode 
    not this
    Qt Code:
    1. #include "qglobal.h"
    2. #if QT_VERSION >= 0x050000
    3. // Qt5 code
    4. #include <QtWidgets>
    5. #else
    6. // Qt4 code
    7. #include <QtGui>
    8. #endif
    To copy to clipboard, switch view to plain text mode 
    The entry in the PRO file is needed either way.
    Last edited by ChrisW67; 16th March 2013 at 23:44.

Similar Threads

  1. compiling
    By InterFiction in forum Newbie
    Replies: 10
    Last Post: 17th November 2011, 07:27
  2. compiling qt in 64b on mac
    By eric_vi in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2010, 14:42
  3. About Compiling
    By joecole in forum Installation and Deployment
    Replies: 1
    Last Post: 15th February 2009, 07:51
  4. Compiling 4.4.1 under Mac OSX 10.5.4
    By smwhit in forum Installation and Deployment
    Replies: 0
    Last Post: 13th August 2008, 22:05
  5. Compiling qt
    By Salazaar in forum Installation and Deployment
    Replies: 1
    Last Post: 4th May 2007, 21:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.