Results 1 to 9 of 9

Thread: Update progress bar in another thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 10:28.

Similar Threads

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