Results 1 to 3 of 3

Thread: Can Qt4 applications be built with C++11?

  1. #1
    Join Date
    Jan 2012
    Location
    San Diego, California
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Can Qt4 applications be built with C++11?

    I'm trying to use C++14 (or C++11) in a small Qt4 application. I have, for example,

    Qt Code:
    1. auto layout = new QHBoxLayout;
    To copy to clipboard, switch view to plain text mode 

    instead of the usual

    Qt Code:
    1. QHBoxLayout *layout = new QHBoxLayout;
    To copy to clipboard, switch view to plain text mode 

    Compiling with g++ (Arch Linux, upgraded about a month ago) gives errors (copied by hand; copy/paste not working with my xterm today):

    Qt Code:
    1. x.cpp:31:2: warning: 'auto' changes meaning in C++11; please remove it
    2. auto layout = new QHBoxLayout;
    3. ^
    4.  
    5. x.cpp:31:7: error: 'layout' does not name a type
    6. auto layout = new QHBoxLayout;
    7. ^
    To copy to clipboard, switch view to plain text mode 

    Perhaps there's something wrong with the way I wrote the line with 'auto', or perhaps Qt4 isn't compatible with using C++11. I did find documentation for qmake (I'm actually using qmake-qt4) and the CONFIG variable, so I put this into the .pro file:

    Qt Code:
    1. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 
    Is there more I need to do? Am I a fool to try using C++11 or C++14?

    Does Qt5 deal with C++11 the same way as Qt4?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can Qt4 applications be built with C++11?

    This doesn't look like it has to do with Qt4 or Q5, the compiler complains about auto.
    But you can easily verify that by using "new int" instead.

    What you need to ensure, however, is that the C++ compiler is aware of your usage of C++11/14.

    For GCC that is the compiler flag -std=c++0x

    Build system generators can have support for doing that correctly for each compiler they support, for example Qt5's qmake can do that by specifying
    CONFIG += c++11

    In CMake you can do
    set(CMAKE_CXX_STANDARD 11)

    Cheers,
    _

  3. #3
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can Qt4 applications be built with C++11?

    Hello,

    yes, you can use C++11 together with Qt4. Of course Qt4 classes do not support C++11 "features" like using initializer lists in constructors. However, it is possible to use e.g. range based loops on Qt4 containers like QVector or QList. To enable C++11 compiler flag (as anda_skoa already pointed out) via qmake use
    Qt Code:
    1. QMAKE_CXXFLAGS += -std=c++11
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QMAKE_CXXFLAGS += -std=c++0x
    To copy to clipboard, switch view to plain text mode 
    depending on your compiler.

    Best regards
    ars

Similar Threads

  1. How to built ?
    By hungpham90 in forum Qt Programming
    Replies: 0
    Last Post: 11th October 2011, 07:50
  2. Replies: 3
    Last Post: 11th May 2010, 04:50
  3. qwt cannot be built
    By estradasphere in forum Qt-based Software
    Replies: 1
    Last Post: 20th March 2010, 18:43
  4. Built EXE
    By vinny gracindo in forum Newbie
    Replies: 4
    Last Post: 3rd February 2010, 17:39
  5. Applications built for different QT versions
    By Asperamanca in forum Installation and Deployment
    Replies: 10
    Last Post: 10th July 2009, 15:36

Tags for this Thread

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.