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. QtConcurrent headers ?
QtWS25 Last Chance

QtConcurrent headers ?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 13.2k Views
  • 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.
  • K Offline
    K Offline
    kegon
    wrote on last edited by
    #1

    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 https://qt-project.org/doc/qt-5.0/qtcore/threads-qtconcurrent.html 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 ;)

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      It's not as obvious to find as the rest of the docs, but there's this: "QtConcurrent":http://qt-project.org/doc/qt-5.0/qtconcurrent/qtconcurrent.html
      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:
      @
      //blocking
      auto future = QtConcurrent::run( { return 42; } );
      qDebug() << future.result();

      //non-blocking
      auto fw = new QFutureWatcher<int>();
      connect(fw, &QFutureWatcher<int>::finished, ={
      qDebug()<<fw->future().result();
      fw->deleteLater();
      });
      fw->setFuture( QtConcurrent::run( { return 42; } ) );
      @

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kegon
        wrote on last edited by
        #3

        Thanks for the link.

        There are some nice simple examples here:

        https://qt-project.org/videos/watch/enhancing_your_qt_application_with_multiple_threads

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

        http://www.develer.com/promo/workshop/enhancing_with_multi_threading.pdf

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kegon
          wrote on last edited by
          #4

          It seems that you do need QT += concurrent

          See the thread https://qt-project.org/forums/viewthread/27021/

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bodzio131
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • B Offline
                B Offline
                beantowel
                wrote on last edited by
                #7

                @kegon I'm using qt5 with cmake, I tried this command:

                find_package(Qt5 COMPONENTS Widgets Concurrent REQUIRED PATHS ... NO_DEFAULT_PATH)
                

                but it didn't work, I still have to use #include <QtConcurrent/QtConcurrent>

                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