Results 1 to 8 of 8

Thread: Thread updates progress bar

  1. #1
    Join Date
    Oct 2009
    Posts
    6
    Platforms
    MacOS X Unix/X11 Windows

    Default Thread updates progress bar

    Hi guys

    I've created a small window (derived from QWidget) with a QProgressBar inside. Then I've created a thread class (derived from QThread). When i launch my file I create the window and the start the thread with an overloaded run method. I want that the thread (during the run) updates the progress bar at each operation. I've thought to the slots and signals system but I have problems: I've implemented a slot in the window class that updates the value of the progress bar and a signal in the thread class that invokes the slot method. In detail:

    Qt Code:
    1. class Window: public QWidget {
    2.  
    3. data and methods
    4.  
    5. public slots:
    6. void updatePB(int);
    7.  
    8. };
    9.  
    10. void Window::updatePB(int val) { pb->setValue(val); }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Thread: public QThread {
    2.  
    3. data and methods
    4.  
    5. Window* pointerWindow;
    6.  
    7. signals:
    8. void signalUpdate(int);
    9. };
    10.  
    11. Thread::Thread (Window* w) {
    12. pointerWindow = w;
    13. connect (this,SIGNAL(signalUpdate(int)), pointerWindow, SLOT(updatePb(int)));
    14. }
    15.  
    16. void Thread::run() {
    17.  
    18. while (true) emit signalUpdate(30);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now the questions are:
    a) why doesn't it compile? It returns the error: no maching function for call to Thread::connect(Thread* const,const char*, Setup*&,const char*). I think the problem is the pointer to the window but I don't know why (I create the thread with: "Thread* th=new Thread(this);"
    b) should I define the signal method?

    Thanks a lot =)

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread updates progress bar

    First of all, use Q_OBJECT macro in you class declaration.

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread updates progress bar

    Hi, maybe the Q_OBJECT macro is missing in your Thread class?

    Ginsengelf

  4. #4
    Join Date
    Oct 2009
    Posts
    6
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Thread updates progress bar

    The Q_OBJECT macro is both in thread and in window class

  5. #5
    Join Date
    Sep 2009
    Posts
    36
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Thread updates progress bar

    Do you have an
    Qt Code:
    1. #include <QThread>
    To copy to clipboard, switch view to plain text mode 
    and did you read QThread doc? QThread is rather complex (in comparsion to ANSI C++ / simple QWidgets)

  6. #6
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Thread updates progress bar

    Quote Originally Posted by GianMarco View Post
    The Q_OBJECT macro is both in thread and in window class
    It does not matter if Q_OBJECT macro is in thread and in window class too. Whereever you need to use Qt based functionalities you need to use this macro. since Signal/Slot is Qt functionality you need to use. As said by others, use #include <QThread> or #include <QObject>.

  7. #7
    Join Date
    Jan 2008
    Location
    Bengaluru
    Posts
    144
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Thread updates progress bar

    Even I tried to set up a progress bar using multi threaded environment and communicated using signal/slot between main thread and other worker thread but it did not updated.

    Later I made it in one GUI main thread only using setValue(...) method. This is useful only for sequence based progress updates i.e., calling setValue(...) everywhere step by step.

  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Thread updates progress bar

    I'm using the same technique to display the progress of loading a large file. What I do differently is that I handle the connections in the main Widget after instantiating the thread and then I connect the thread's signal directly to the progress bar's slot like this:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. FileLoader* loader = new FileLoader();
    7. loader->start();
    8. connect(loader, SIGNAL(fileLoadingProgress(int)), ui.progressBar, SLOT(setValue(int)));
    9. }
    To copy to clipboard, switch view to plain text mode 

    MyWidget is a QWidget anyways, but my FileLoader is my own class, so it has the Q_OBJECT defined in the header. And all it does is basicly:

    Qt Code:
    1. void FileLoader::run()
    2. {
    3. load a piece of the file
    4.  
    5. emit fileLoadingProgress(100*bytesLoaded/bytesTotal);
    6. }
    To copy to clipboard, switch view to plain text mode 


    Hope that helps.
    Cruz

Similar Threads

  1. busy progress bar without thread ?
    By npc in forum Newbie
    Replies: 34
    Last Post: 23rd July 2009, 10:29
  2. Showing progress bar while worker thread works
    By olidem in forum Qt Programming
    Replies: 6
    Last Post: 27th April 2009, 21:43
  3. Concurrent progress reporting
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2008, 09:47
  4. Display progress on another thread
    By radu_d in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 09:02
  5. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13

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.