Results 1 to 3 of 3

Thread: Pause at each row in QTextEdit

  1. #1
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Pause at each row in QTextEdit

    Hi,

    I would like to write some row's of text into a QTextEdit.
    At each row there should be a pause (e.g. x seconds).
    Example:
    Qt Code:
    1. void MyClass::startUpdate(void)
    2. {
    3. ui->textEdit->setText("*** Updating Software ***");
    4. // Pause about 1 sec.
    5. // Call system functions to update the software
    6. ui->textEdit->setText("*** Now I'm call system functions to update the software ***");
    7. // Pause about 1 sec.
    8. // Reboot
    9. ui->textEdit->setText("*** Now the system will be rebooted... ***");
    10. // Pause about 1 sec.
    11. }
    To copy to clipboard, switch view to plain text mode 
    I can't use sleep functions because they block the thread, which means the paint event won't be called at each ui->textEdit->setText("bla bla") call. Hence startUpdate(void) is blocked for all sleep calls and when they finished then QTextEdit will be painted.

    My last try is with a QTimer:
    Qt Code:
    1. void MyClass::startUpdate(void)
    2. {
    3. m_sMessage = "Simulate the software update.";
    4. QTimer::singleShot(1000, this, SLOT(slotAppendText()));
    5.  
    6. m_sMessage = "Simulate the software update 2.";
    7. QTimer::singleShot(1000, this, SLOT(slotAppendText()));
    8. }
    9.  
    10. void MyClass::slotAppendText()
    11. {
    12. ui->textEdit->append(m_sMessage);
    13. }
    To copy to clipboard, switch view to plain text mode 

    But with a QTimer both "singleShot" call slotAppendText(). Of couse I could set the second QTimer to 2000ms, but I want to simulate that it takes time to perform the "update" step by step.

    I'm glad for any hints.

    - lamb

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

    Default Re: Pause at each row in QTextEdit

    Qt Code:
    1. QStringList linesOfText;
    2. linesOfText << "Simulate the softwre update.";
    3. linesOfText << "Simulate the softwre update 2.";
    4.  
    5. QTimer::singleShot(1000, this, SLOT(appendText()));
    6.  
    7. // ...
    8.  
    9. void X::appendText() {
    10. ui->textEdit->append(linesOfText.takeFirst());
    11. if(!linesOfText.isEmpty())
    12. QTimer::singleShot(1000, this, SLOT(appendText()));
    13. }
    To copy to clipboard, switch view to plain text mode 
    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
    Mar 2012
    Posts
    30
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pause at each row in QTextEdit

    Thank you,

    I solved this problem like you. I added a enum to MyClass:
    Qt Code:
    1. enum current_step {
    2. Start,
    3. Step1,
    4. Step2
    5. };
    To copy to clipboard, switch view to plain text mode 
    And I use this enum in the slot function:
    Qt Code:
    1. MyClass::slotSoftwareUpdate()
    2. {
    3. switch(m_nCurrentStep)
    4. {
    5. case (Start):
    6. {
    7. msg = "*** Updating Software ***";
    8. ui->textEdit->setText(msg);
    9. m_nCurrentStep = Step1;
    10. QTimer::singleShot(1000, this, SLOT(slotSoftwareUpdate()));
    11. break;
    12. }
    13. case (Step1):
    14. {
    15. msg = "*** Updating Software Step1 ***";
    16. ui->textEdit->setText(msg);
    17. m_nCurrentStep = Step2;
    18. QTimer::singleShot(1000, this, SLOT(slotSoftwareUpdate()));
    19. break;
    20. }
    21. case (Step2):
    22. {
    23. msg = "*** Updating Software Step2 ***";
    24. ui->textEdit->setText(msg);
    25. m_nCurrentStep = Start;
    26. QTimer::singleShot(1000, this, SLOT(slotSoftwareUpdate()));
    27. break;
    28. }
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. how to pause or resume a download??
    By Ali Reza in forum Newbie
    Replies: 3
    Last Post: 11th June 2012, 20:11
  2. QThread - how to pause and resume
    By xrep in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2012, 08:59
  3. I need a pause (QTimer)
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2008, 05:37
  4. Replies: 1
    Last Post: 14th June 2007, 15:52
  5. Pause executing QProcess
    By bravetanveer in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2006, 07:55

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.