Results 1 to 6 of 6

Thread: QtConncurrent - calling function within the class

  1. #1
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Wink QtConncurrent - calling function within the class

    Hello everyone.

    I need to call a function from another function in the same class. As it requires much time to be finished (running mysql query on external server), I want it to be in another thread. My code is sth like that:
    Qt Code:
    1. void runQuery(){
    2. ...
    3. }
    4.  
    5. void getProducts(){
    6. QtConcurrent::run(runQuery);
    7. }
    To copy to clipboard, switch view to plain text mode 

    I get an error:

    Qt Code:
    1. no matching function to call to 'run(<unknown type>)'
    To copy to clipboard, switch view to plain text mode 

    Could you help me out please?

  2. #2
    Join Date
    Oct 2009
    Posts
    37
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: QtConncurrent - calling function within the class

    Look into QThread, not QtConcurrent.
    Disclaimer: Although I work on Qt for Nokia, anything I post here is personal

  3. #3
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtConncurrent - calling function within the class

    Quote Originally Posted by mgoetz View Post
    Look into QThread, not QtConcurrent.
    Still don't know how to use it in my class. I will appreciate some tips and hints.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConncurrent - calling function within the class

    Qt Code:
    1. #include <boost/bind.hpp>
    2.  
    3. void YourClassName::getProducts(){
    4. QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );
    5. }
    To copy to clipboard, switch view to plain text mode 
    obviously, you need to take care that runQuery() and your class are "thread safe" because they will run concurrently...

    For details on Boost and Boost.Bind see www.boost.org

    HTH

  5. The following user says thank you to caduel for this useful post:

    cdguenther (4th June 2010)

  6. #5
    Join Date
    Mar 2008
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QtConncurrent - calling function within the class

    Quote Originally Posted by caduel View Post
    Qt Code:
    1. #include <boost/bind.hpp>
    2.  
    3. void YourClassName::getProducts(){
    4. QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );
    5. }
    To copy to clipboard, switch view to plain text mode 
    obviously, you need to take care that runQuery() and your class are "thread safe" because they will run concurrently...

    For details on Boost and Boost.Bind see www.boost.org

    HTH
    Thanks for input, but the solution isn't working. It compiles without errors and warnings, program works, but not properly. It doesn't get results from a database. Isn't a new instance of my class being created by QtConcurrent::run( boost::bind(&YourClassName:runQuery, this) );? What can be the couse?

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QtConncurrent - calling function within the class

    no, boost::bind creates a function object by binding (here runQuery()) a member function to an object (here this). So it exeutes runQuery() on the very object you call getProducs() on.

    You can also pass YourClassName() instead of this, then you create a temporary that is used for executing. (Also note that you might need a separe db connection if you are using the db from the main thread, too.)

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. Regading Driver to connect Postgresql Database
    By dummystories in forum Installation and Deployment
    Replies: 38
    Last Post: 12th March 2009, 08:19
  3. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  4. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  5. Problems calling C function in C++/Qt class
    By Rayven in forum General Programming
    Replies: 2
    Last Post: 2nd June 2006, 22:32

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.