Results 1 to 17 of 17

Thread: update the QProgressBar

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    Quote Originally Posted by wysota View Post
    Should using signals and slots not be possible in your case, use events instead - post a custom event to the progress bar and handle the event there changing the value shown by the bar. Also remember not to starve the event loop or you won't see any progress on the bar as it will not update.
    Thanks for your reply, i'll check out the events. If I'm correct, using events, I don't have to litter my library code with any qt related code and I just have to send every second (depending on the timer) a custom event to the progressbar.

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

    Default Re: update the QProgressBar

    With signals and slots you don't have to litter your code as well. You can always call the setValue() method (if used from the main thread) or use QMetaObject::invokeMethod with QueuedConnection parameter (from worker threads).
    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.


  3. #3
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    This is just some test code:

    Qt Code:
    1. timerThread::timerThread() {
    2. timer = new QTimer();
    3. connect( timer, SIGNAL(timeout()), this, SLOT(sendValue()));
    4. }
    5.  
    6. void timerThread::setValues(QProgressBar * s, int * state) {
    7. status=state;
    8. theBar=s;
    9. }
    10.  
    11.  
    12. void timerThread::run() {
    13. timer->setInterval(1000);
    14. timer->start();
    15. }
    16. */
    17. }
    18.  
    19. // this will be executed when the timer has finished
    20. void timerThread::sendValue() {
    21. if (*status <= 100) {
    22. timer->start();
    23. }
    24. theBar->setValue(*status);
    25. }
    To copy to clipboard, switch view to plain text mode 

    I can't see how you think this will work without putting the timer in a thread.
    As the timer and the filter process have to run simultaneously.
    Now i get an error when i run it: it cant seem to find the slot 'sendValue()'
    Qt Code:
    1. Object::connect: No such slot QThread::sendValue() in timerThread.cpp:6
    To copy to clipboard, switch view to plain text mode 

    Any ideas?
    Last edited by aloha; 15th April 2009 at 20:09.

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

    Default Re: update the QProgressBar

    I'm guessing you forgot to add the Q_OBJECT macro to the thread class but the real problem is accessing the widget from within a worker thread which is forbidden. Also remember the thread object lives in the thread that created the object and not in the thread that the object controls. Therefore slots from this object will be executed in context of the main thread which is probably blocked by some heavy calculation or else you wouldn't be spawning the worker thread at all.
    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.


  5. #5
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    I cant seem to find much information about Qevents.
    I think i still need signal and slot communication in my timer because I've got to know when the timer time's out. Or can I also do that using slots, maybe?

    Imagine that this works, then i still need to send a slot to the mainwindow. Which i had to pass on by pointer to the thread, so then i guess I'm still accessing a widget within a worker thread which is forbidden?

    Looking at my previous post, can anybody tell me where just to place those QEvents, cause it seems harder than expected.

    Thank you!

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

    Default Re: update the QProgressBar

    Quote Originally Posted by aloha View Post
    I think i still need signal and slot communication in my timer because I've got to know when the timer time's out. Or can I also do that using slots, maybe?
    Yes, you can. See QObject::startTimer(), QObject::killTimer() and QObject::timerEvent().

    Imagine that this works, then i still need to send a slot to the mainwindow. Which i had to pass on by pointer to the thread, so then i guess I'm still accessing a widget within a worker thread which is forbidden?
    Sending a signal doesn't involve accessing the receiver directly. Only making the connection does but it is thread safe.

    Looking at my previous post, can anybody tell me where just to place those QEvents, cause it seems harder than expected.
    See docs for QObject::customEvent() and follow the links.
    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.


  7. #7
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    In my filter call I know have the following:
    Qt Code:
    1. state=new double(0); // initialize the member variable
    2. cout << *state << endl; // prints 0
    3. updateProgressBar * me=new updateProgressBar(state, this); // this holds the timer and the events with a pointer to the state
    4.  
    5. ds->Filter(min, max,s, state); // pass the same pointer to state to check on the progress
    To copy to clipboard, switch view to plain text mode 

    the updateProgressBar code:
    Qt Code:
    1. updateProgressBar::updateProgressBar(double* state, QObject *parent)
    2. : status(state), pa(parent), QObject(parent) // initialize
    3. {
    4. startTimer(20); // update every 20 miliseconds
    5. }
    6.  
    7. // this will be called when the timer times out
    8. void updateProgressBar::timerEvent(QTimerEvent *event)
    9. {
    10. std::cout << "Timer ID:" << event->timerId() << std::endl;
    11. if (*status==100) { // finished?
    12. killTimer(event->timerId());
    13. }
    14. std::cout << "sending " << *status << std::endl; // !!!
    15. const QEvent::Type MyEvent = (QEvent::Type)1234;
    16. QEvent * message=new QEvent(MyEvent);
    17. QCoreApplication::sendEvent(pa, message);
    18. }
    To copy to clipboard, switch view to plain text mode 

    And i receive the event in my parent frame:

    Qt Code:
    1. void intensityframe2::customEvent(QEvent * events) {
    2. cout << "received an event" << endl;
    3. int value=(*state); //cast to int
    4. m_ui->progressBar->setValue(value); //display on the screen
    5. }
    To copy to clipboard, switch view to plain text mode 
    This code works, but i've got one pointer problem.
    After even one timer loop, i display the value of the pointer (see !!!) and then i send an event to the parent window so he knows he has to check on the value of the state/progress and display it. But even when no assignments have been made to value of the pointer is set to a strange value: like for example: 2.1305e-313

    The value of that double is allocated on the heap, so i dont immediately see a reason why the value of that pointer isnt just 0 and not 2.1305e-313 or so...

    ps: even when i place the timer in my mainwindow and delete the updateProgressBar class, the value gets changed :/
    Last edited by aloha; 17th April 2009 at 13:19.

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

    Default Re: update the QProgressBar

    I don't understand why you are passing that pointer to double around.
    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.


  9. #9
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    the filtering is done in my library, i'm running this operation through the command
    ds->Filter(...);
    how else can I know how much computation is already completed without passing a pointer?
    The filteroperation is a function, not an object, so i cant have a public member function or so.
    During the filter process i can update that status (pointer to double) and in my gui i have a timer who checks the value of that pointer every 0.2 seconds.

    I need to have access to the same value from two sections of my application.
    1) Write access for the filterfunction, to update the status of the progress
    2) read access for the gui to read the value of the current progress

    Don't think there is another decent/better way to accomplish this

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

    Default Re: update the QProgressBar

    Quote Originally Posted by aloha View Post
    how else can I know how much computation is already completed without passing a pointer?
    That's what the setValue() method in the progress bar is for, isn't it?

    The filteroperation is a function, not an object, so i cant have a public member function or so.
    During the filter process i can update that status (pointer to double) and in my gui i have a timer who checks the value of that pointer every 0.2 seconds.
    Let's temporarily forget accessing a shared variable from within multiple threads is WRONG... Updating the variable (why is it created on heap anyway?) won't do you any good because you have to inform the environment that you have actually changed it. You try to get away from it by wasting computer cycles with the timer but it's a bad approach. You should say "hey, my progress value has changed to 3.14!" instead. You can do that either by emitting a signal or by posting an event that carries the value (the latter is probably better in your case as you can do that from an arbitrary point in your code).

    Don't think there is another decent/better way to accomplish this
    I hate to be rude but the one you picked is far from being decent. What happens if you read the value from one thread while it is being overwritten by the other thead? You'll get a bogus value. In your case it's not that dangerous, you'll just get a wrong progress value for a while but in a general case this can blow up the world.
    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.


  11. #11
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    1

    Default Re: update the QProgressBar

    Quote Originally Posted by wysota View Post
    That's what the setValue() method in the progress bar is for, isn't it?



    Let's temporarily forget accessing a shared variable from within multiple threads is WRONG... Updating the variable (why is it created on heap anyway?) won't do you any good because you have to inform the environment that you have actually changed it. You try to get away from it by wasting computer cycles with the timer but it's a bad approach. You should say "hey, my progress value has changed to 3.14!" instead. You can do that either by emitting a signal or by posting an event that carries the value (the latter is probably better in your case as you can do that from an arbitrary point in your code).



    I hate to be rude but the one you picked is far from being decent. What happens if you read the value from one thread while it is being overwritten by the other thead? You'll get a bogus value. In your case it's not that dangerous, you'll just get a wrong progress value for a while but in a general case this can blow up the world.
    Ok , let's start again, i've got no threads. I've got a mainwindow that starts a filterfunction. Now i want to add a progressbar reporting the progress of the filteroperation.
    The filterfunction is in another file. how can i have access to the state of the progress of the filterfunction? My suggestion was to pass an integer/double by reference/pointer that I update during the filter progress. For example, i filter 5 different things, so the value of that integer/double will be incremeted every time by 20 (0%, 20%, 40% .... 100%)

    Now, to update the progressbar on the screen, i need a timer to check like every 0.2 seconds the value of that passed integer/double. That's why i need the QTimer.
    And i use the QTimerEvent to know when the timer has timed out and when to update the value on the screen.

    If you can tell me another way how to check on the status/progress of a procedure without passing an integer/double that holds the status, please do...

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

    Default Re: update the QProgressBar

    Use signals and slots.
    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. Problem in QProgressBar update
    By nikhilqt in forum Qt Programming
    Replies: 0
    Last Post: 10th March 2009, 10:20
  2. Qt Update project - Opinions wanted
    By pvdk in forum Qt Programming
    Replies: 0
    Last Post: 8th November 2008, 08:41
  3. Paintevent and update
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 07:09
  4. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 09:42
  5. QSlider Update Problem
    By December in forum Qt Programming
    Replies: 4
    Last Post: 10th September 2006, 04: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
  •  
Qt is a trademark of The Qt Company.