Results 1 to 11 of 11

Thread: Creating thread for each function of a class

  1. #1
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating thread for each function of a class

    Hi there. I'm just a beginner in Qt and I have been wrapping my head about this question for quite a while. Could you please help me out?

    I have got a class, named, let's say,
    Qt Code:
    1. class BaseClass: public Thread
    To copy to clipboard, switch view to plain text mode 
    I have a bunch of functions and I want each function to run in a separate thread. All information I have stumbled across so far claims that if I want to run a function in a separate thread I have to create a class
    Qt Code:
    1. class Thread: public QThread
    To copy to clipboard, switch view to plain text mode 
    that inherits QThread and override QThread::run() function. But I hope there is another solution apart from creating a class which inherits QThread for each function in BaseClass. I would appreciate any ideas on this. Thank you so much in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Creating thread for each function of a class


  3. The following user says thank you to Lykurg for this useful post:

    Astrologer (17th April 2010)

  4. #3
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating thread for each function of a class

    Thank you, it is exactly what I was craving for
    Last edited by Astrologer; 17th April 2010 at 09:44.

  5. #4
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating thread for each function of a class

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(this->ui->button1, SIGNAL(clicked()), this, SLOT(startThread()));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. void MainWindow::startThread()
    31. {
    32. QFuture<void> f1 = QtConcurrent::run(mbox);
    33. f1.waitForFinished();
    34. }
    35.  
    36. void mbox()
    37. {
    38. for (int i=0; i < 100000; i++)
    39. {
    40. qDebug() << i;
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    After this, GUI dies. I don't know what to do with it. Furthermore, there is a limit of input arguments, which is 5. What am I to do if there has to be more than that? Might the problem of hanging be in using f1.waitForFinished()?
    Last edited by Astrologer; 19th April 2010 at 17:45.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Creating thread for each function of a class

    What do you mean by dies? Does your application crash or is it just not respondable? The later would be because of waitForFinished() since your code does nothing else than normally call the mbox function with blocking the gui thread. If you need more arguments use a QVariantList.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Creating thread for each function of a class

    Using QFutureWatcher is asynchronous. So connect the finished signal of f1 to a slot where you can go on, after the work in the function is done.

  8. #7
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating thread for each function of a class

    Quote Originally Posted by Lykurg View Post
    What do you mean by dies? Does your application crash or is it just not respondable? The later would be because of waitForFinished() since your code does nothing else than normally call the mbox function with blocking the gui thread. If you need more arguments use a QVariantList.
    No, it doesn't respond. So, I excluded "waitFor" and GUI does respond. Without "wait" I would use final signal and some slot as you suggested and go on with whatever is needed and it will work, won't it? You meant QList<QVariant>, didn't you? Thank you.

  9. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Creating thread for each function of a class

    Quote Originally Posted by Astrologer View Post
    You meant QList<QVariant>, didn't you? Thank you.
    Yes, since Qt does a typedef, QVariantList == QList<QVariant>. (QVariant::QVariantList)

  10. #9
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating thread for each function of a class

    Quote Originally Posted by Lykurg View Post
    Using [QTCLASS]So connect the finished signal of f1 to a slot where you can go on, after the work in the function is done.
    If I do like this:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(this->ui->button1, SIGNAL(clicked()), this, SLOT(startThread()));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::changeEvent(QEvent *e)
    19. {
    20. QMainWindow::changeEvent(e);
    21. switch (e->type()) {
    22. case QEvent::LanguageChange:
    23. ui->retranslateUi(this);
    24. break;
    25. default:
    26. break;
    27. }
    28. }
    29.  
    30. void MainWindow::startThread()
    31. {
    32. QFuture<void> f1 = QtConcurrent::run(this, &MainWindow::mbox);
    33. //f1.waitForFinished();
    34. }
    35.  
    36. void MainWindow::mbox()
    37. {
    38. for (int i=0; i < 20000; i++)
    39. {
    40. qDebug() << i;
    41. }
    42.  
    43. emit done();
    44. }
    45.  
    46. void MainWindow::done()
    47. {
    48. QMessageBox::aboutQt(this, "Done!");
    49. }
    To copy to clipboard, switch view to plain text mode 

    It apparently tries and create widget from within the thread and fails. Could you please hint me?
    Last edited by Astrologer; 19th April 2010 at 18:19. Reason: solved partly

  11. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Creating thread for each function of a class

    First you have to declare done as a slot then I meant to use the QFutureWatcher signal finished! E.g.:
    Qt Code:
    1. QFutureWatcher<void> watcher;
    2. connect(&watcher, SIGNAL(finished()), this, SLOT(done()));
    3. //...
    4. QFuture<void> f1 = QtConcurrent::run(this, &MainWindow::mbox);
    5. watcher.setFuture(f1);
    To copy to clipboard, switch view to plain text mode 
    (not tested, but you see the idea!)

  12. The following user says thank you to Lykurg for this useful post:

    Astrologer (19th April 2010)

  13. #11
    Join Date
    Apr 2010
    Location
    Moscow
    Posts
    31
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Creating thread for each function of a class

    Yes, I do. I think I will peep in again just in case. Thank you for your patience and great help

Similar Threads

  1. Replies: 16
    Last Post: 7th October 2009, 08:17
  2. creating treeWidgetItem using emitted signal from thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 08:37
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. How i will use treeWidget of GUI class in thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2007, 15:31
  5. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:05

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.