Important: Please read the Qt Code of Conduct -

QtConcurrent headers ?

  • I'm venturing into QtConcurrent for the first time and only have a little experience with threads.

    I had to use this include (Qt 5.0.2):

    #include <QtConcurrent/QtConcurrentRun>

    Is this correct ?
    I tried

    #include <QtConcurrentRun>
    #include <QtConcurrent>
    #include <QConcurrent>

    but they did not work for me.

    A few comments:

    The documentation is frustratingly minimal about using QtConcurrent. As a namespace of Qt, nowhere does it say the form of the above include and QtConcurrent doesn't come under the documentation class index.

    In the QtConcurrent namespace functions are not linked.

    This page also says that QtConcurrent is a module. That suggests that your project file needs a line of QT += concurrent. But this is not the case.

    There is a need for some simple examples (preferably Qt 5) that do not talk about mutexes or thread pools or other thread related issues; just a simple explanation that you can write a non-blocking function that will be executed asynchronously and you can have a callback via signals/slots. Then various examples of forms of external functions, member functions, static member functions, arguments, maps etc.

    Also please mention somewhere that QFuture can be created on the stack but QFutureWatcher is created on the heap and you should delete it when you are done.

    If someone could please write a nice article about this I think it would be very useful ;)

  • Moderators

    It's not as obvious to find as the rest of the docs, but there's this: "QtConcurrent":
    It's a bit of a stripped down version from Qt 4.8

    It is part of the core module, so there's no need for additional QT+=
    The doc states that it should work by including <QtConcurrent> but it's actually <QtConcurrent/QtConcurrent>

    As for a simple example, there's not really much to it:
    auto future = QtConcurrent::run( { return 42; } );
    qDebug() << future.result();

    auto fw = new QFutureWatcher<int>();
    connect(fw, &QFutureWatcher<int>::finished, ={
    fw->setFuture( QtConcurrent::run( { return 42; } ) );

  • Thanks for the link.

    There are some nice simple examples here:

    Unfortunately, the video is not online. I found the slides here:

  • It seems that you do need QT += concurrent

    See the thread

  • Moderators

    That's odd. I tried my examples without it and it worked.

  • You both are right :) Qt4.8 contains concurrent implementation in QtCore module, but Qt5 moved it into separated library :)

Log in to reply