PDA

View Full Version : Can Qt4 applications be built with C++11?



darenw
26th February 2016, 09:05
I'm trying to use C++14 (or C++11) in a small Qt4 application. I have, for example,


auto layout = new QHBoxLayout;

instead of the usual


QHBoxLayout *layout = new QHBoxLayout;

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


x.cpp:31:2: warning: 'auto' changes meaning in C++11; please remove it
auto layout = new QHBoxLayout;
^

x.cpp:31:7: error: 'layout' does not name a type
auto layout = new QHBoxLayout;
^

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:


CONFIG += c++11

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?

anda_skoa
26th February 2016, 09:42
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,
_

ars
26th February 2016, 10:23
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
QMAKE_CXXFLAGS += -std=c++11 or
QMAKE_CXXFLAGS += -std=c++0x depending on your compiler.

Best regards
ars