Results 1 to 5 of 5

Thread: How to start a thread (inline)

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default How to start a thread (inline)

    I am aware that to execute something in a separate thread, one can simply extend QThread, implement run(), and write the pertinent code in the run method. But is there a way to execute an arbitrary block of code in a separate thread w/o having to create a whole new class? For example, maybe when I click a button I want a task to be executed in a separate thread, etc. Thanks.

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to start a thread (inline)

    Quote Originally Posted by DiamonDogX View Post
    I am aware that to execute something in a separate thread, one can simply extend QThread, implement run(), and write the pertinent code in the run method. But is there a way to execute an arbitrary block of code in a separate thread w/o having to create a whole new class? For example, maybe when I click a button I want a task to be executed in a separate thread, etc. Thanks.
    Use OpenMP
    Qt Code:
    1. #pragma omp parallel default(none) shared(n,a,b,c,d) private(i)
    2. {
    3. #pragma omp for nowait
    4. for (i=0; i<n-1; i++) b[i] = (a[i] + a[i+1])/2;
    5.  
    6. #pragma omp for nowait
    7. for ( i=0; i<n; i++ ) d[i] = 1./c[i];
    8.  
    9. } /* -- End of parallel region -- */
    To copy to clipboard, switch view to plain text mode 

    Or google search for QtConcurrent

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to start a thread (inline)

    See QRunnable and QThreadPool or better yet QtConcurrent::run.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to start a thread (inline)

    While we're on the subject, if I am executing code in another thread (i.e. a thread that is not the UI thread), is there a way to manually execute a piece of code on the UI thread?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to start a thread (inline)

    Yes. Either post an event to the main thread and catch it from within QObject::customEvent() of the object living in the main thread or use QMetaObject::invokeMethod() with Qt::QueuedConnection as the last parameter. The method you call has to be a slot.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Thread Help
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 22:10
  2. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. QThread : run() Vs start()
    By cs_raja in forum Qt Programming
    Replies: 4
    Last Post: 21st November 2006, 10:36
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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.