Results 1 to 3 of 3

Thread: Really need a way to process events!

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs down Really need a way to process events!

    QApplication::processEvents() or QApplication::processEvents(QEventLoop::AllEvents) doesn't seem to work at all with me for an unknown reason.
    I have a void function that executes some commands and another one that updates the progressbar while the commands are executed, which can take a little time to execute. So the code looks like:
    Qt Code:
    1. //void function that executes the commands:
    2. update_bar("Executing command one",0);
    3. system("command1");
    4. update_bar("Executing command two",25);
    5. system("command2");
    6. ..
    7. ..
    8. update_bar("Done",100);
    To copy to clipboard, switch view to plain text mode 
    the update_bar function is:
    Qt Code:
    1. void update_bar(QString txt, int value){
    2. ui->progressBar->setValue(value);
    3. ui->progressBar->setFormat(txt);
    4. QApplication::processEvents(QEventLoop::AllEvents);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, while I am setting the value and the format of the progressbar 5 times (at 0,25,50,75 and at 100%), when calling the update_bar function, the progressbar only updates at 50 and 100%, skipping the 0,25 and 75% and their texts..


    QApplication::processEvents is the second time that makes me dizzy on how it works...

    Is there anything available that is more efficient, or do you have to suggest an other way of using this function so as to update?
    Last edited by wysota; 20th May 2011 at 21:45.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Really need a way to process events!

    QCoreApplication:rocessEvents() is nasty. I wouldn't use it for anything (ran into odd issues too often because of it). There are some approaches you could take, but the best is probably to use QProcess with some signal/slot magic. Instead of waiting for the process to finish, the events will be processed. You would get a sort of state machine

    Qt Code:
    1. // connect this slot to QProcess::finished(...)
    2. void MyClass:nextSystemCall()
    3. {
    4. switch (m_state) {
    5. ...
    6. // execute some call depending on state
    7. }
    8. emit progressChanged(progress);
    9. }
    To copy to clipboard, switch view to plain text mode 
    You could consider checking the result values of the executed processes as well.

    This whole approach will rid you of the need to call processEvents(). Disable certain parts of your GUI if you have to.

    In addition, your functions just shouldn't take so long that you need to call processEvents(). Besides that, if processEvents() is called in a slot, you might get in ugly trouble with timing and recurring events, but that's another story.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Really need a way to process events!

    One problem here: your system() call is going to spawn a new shell process every time it is called. This could be fiddling with your timing.

    What happens if you replace the system() call with a call to sleep(5) or something similar?

Similar Threads

  1. ActiveQt out of process control sends no events
    By UweS in forum Qt Programming
    Replies: 0
    Last Post: 16th May 2011, 14:08
  2. Replies: 0
    Last Post: 6th August 2010, 11:01
  3. How to communicate Qt Process with non-qt process
    By nrabara in forum Qt for Embedded and Mobile
    Replies: 9
    Last Post: 15th February 2009, 21:01
  4. Pass custom events to another process
    By TTGator in forum Qt Programming
    Replies: 9
    Last Post: 14th January 2009, 22:02
  5. Replies: 1
    Last Post: 13th September 2008, 14:45

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.