Results 1 to 9 of 9

Thread: Update progress bar in another thread

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Angry Update progress bar in another thread

    Hi all,

    I have an application with an additional thread that handles a heavy process for the purpose of the GUI doesn't lock.

    In this process is needed update a progress bar that resides in the main thread but sometimes the application closes unexpectedly.

    I get these messages in application output:

    QObject::connect: Cannot queue arguments of type 'QTextBlock'
    (Make sure 'QTextBlock' is registered using qRegisterMetaType().)
    QObject::connect: Cannot queue arguments of type 'QTextCursor'
    (Make sure 'QTextCursor' is registered using qRegisterMetaType().)
    QObject::killTimers: timers cannot be stopped from another thread
    How can I solve this issue?

    My code:

    Qt Code:
    1. #ifndef THREADMANAGER_H
    2. #define THREADMANAGER_H
    3.  
    4. #include <QObject>
    5.  
    6. class QThread;
    7. class GComputationWorker;
    8.  
    9. class ThreadManager: public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. // constructors and destructor
    14. /**
    15.   * Returns an instance of this class. When called for the first
    16.   * time, a new instance is created and returned. After that,
    17.   * calling InstanceL returns the same instance that was created
    18.   * earlier.
    19.   *
    20.   * @return A pointer to a ThreadManager object
    21.   */
    22. static ThreadManager* instance();
    23.  
    24. private:
    25. // constructor
    26. /**
    27.   * Default constructor is private because we are using the
    28.   * singleton design pattern.
    29.   */
    30.  
    31. ThreadManager();
    32.  
    33. public:
    34.  
    35. void doComputationWork();
    36.  
    37. private:
    38.  
    39. static ThreadManager *_instance;
    40.  
    41. QThread *computationThread;
    42.  
    43. GComputationWorker *computationWorker;
    44.  
    45. };
    46.  
    47. #endif // THREADMANAGER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "threadmanager.h"
    2. #include "gcomputationworker.h"
    3.  
    4. #include <QThread>
    5.  
    6. ThreadManager * ThreadManager::_instance = NULL;
    7.  
    8. ThreadManager::ThreadManager()
    9. {
    10. _instance = NULL;
    11. computationThread = new QThread;
    12. computationWorker = new GComputationWorker;
    13. computationWorker->moveToThread(computationThread);
    14. }
    15.  
    16. ThreadManager* ThreadManager::instance()
    17. {
    18. if (ThreadManager::_instance == 0)
    19. ThreadManager::_instance = new ThreadManager();
    20. return ThreadManager::_instance;
    21. }
    22.  
    23. void ThreadManager::doComputationWork()
    24. {
    25. computationOperFinished = false;
    26. computationThread->start();
    27. computationThread->setPriority(QThread::NormalPriority);
    28. QMetaObject::invokeMethod(computationWorker, "doWork", Qt::QueuedConnection);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef GCOMPUTATIONWORKER_H
    2. #define GCOMPUTATIONWORKER_H
    3.  
    4. #include <QObject>
    5.  
    6. class GComputationWorker : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10.  
    11. GComputationWorker();
    12.  
    13. ~GComputationWorker();
    14.  
    15. public slots:
    16.  
    17. void doWork();
    18. };
    19.  
    20. #endif // GCOMPUTATIONWORKER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "gcomputationworker.h"
    2.  
    3. GComputationWorker::GComputationWorker()
    4. {
    5. }
    6.  
    7. GComputationWorker::~GComputationWorker()
    8. {
    9. }
    10.  
    11. void GComputationWorker::doWork()
    12. {
    13. // Start computation
    14. // The progress bar is updated by setValue method
    15. }
    To copy to clipboard, switch view to plain text mode 

    Usage:

    Qt Code:
    1. void MainWindow::startRun()
    2. {
    3. ThreadManager::instance()->doComputationWork();
    4. }
    5.  
    6. void MainWindow::onProgressBarValueChanged(int value)
    7. {
    8. if (ui->progressBar->maximum() == value)
    9. {
    10. ui->progressBar->hide();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Regards.
    Last edited by qt_developer; 18th June 2012 at 11:28.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    qregistermetatype: http://qt-project.org/doc/qt-4.8/qme...gisterMetaType

    That will allow you to send those types across thread boundaries.
    As for the timer issue, I'm not sure if that will cause your crash or not, maybe an assert.

    Please give more details about what causes the crash/assert.
    Have you debugged?
    What line is the problem on?
    What is the call stack?


    You show a problem with a timer, but I do not see any code involving timers...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Update progress bar in another thread

    You can't modify GUI objects from another thread.

  4. #4
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    I haven't used timers but I get the error:

    QObject::killTimers: timers cannot be stopped from another thread


    Quote Originally Posted by Lesiok View Post
    You can't modify GUI objects from another thread.
    I haven't modified the GUI directly. I'm using cross thread signal and slot connection to set the progress on the bar.

    Qt Code:
    1. connect(this, SIGNAL(signalUpdate(int)), mainWindowPointer, SLOT(onUpdateProgressBar(int)), Qt::QueuedConnection);
    2. ...
    3. emit signalUpdate(valueToSet);
    To copy to clipboard, switch view to plain text mode 

    What's wrong?

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    What's wrong? First, what is the problem, please Be sure to adjust your code for qRegisterMetaType and then report any changes.

    From our perspective, you have only shown some console output and some code, and said that your app crashes. We cannot be sure (without searching through Qt source code) if those console outputs are related to the app exit or not. So please respond to these pertinent questions already posed:
    Quote Originally Posted by amleto View Post
    Please give more details about what causes the crash/assert.
    Have you debugged?
    What line is the problem on?
    What is the call stack?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    The problem is that the crash is random. Sometimes doesn't occurs.

    The ThreadManager class is implemented properly?

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    Only if it is only accessed via a single thread. It is not thread safe.

    If you have an app with 'random' crash/assert, then throwing up pieces of code and asking, 'is it ok?', is not the best way to resolve the issue.

    You need to debug your app when it crashes and give more information, or give more code. Preferably enough to compile.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    I have solved the problem, I was accessing to a bad memory address, for this reason I got the weird message:

    "QObject::killTimers: timers cannot be stopped from another thread"

    Regards.
    Last edited by qt_developer; 19th June 2012 at 18:06.

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Update progress bar in another thread

    Another case where the OP puts up some code absolutely irrelevant to his problem. Also avoided/evaded questions asking about information obtained from debugging. This is why I like compilable examples
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. set progress bar values inside a thread
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2011, 15:55
  2. Thread updates progress bar
    By GianMarco in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2009, 14:29
  3. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 10:29
  4. Showing progress bar while worker thread works
    By olidem in forum Qt Programming
    Replies: 6
    Last Post: 27th April 2009, 21:43
  5. Display progress on another thread
    By radu_d in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 09:02

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.