Results 1 to 3 of 3

Thread: update a counter in PlainTextEdit on the same line

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default update a counter in PlainTextEdit on the same line

    I have a PlainTextEdit called commandWindow in which I want to display a counter. The counter comes from a thread process. The counter is always on the last line of the PlainTextEdit. I made some test code, the sqrt is to delay the counter:
    Qt Code:
    1. QString out;
    2. for (int i = 0; i < 100; i++)
    3. {
    4. double x;
    5. out = commandWindow->toPlainText();
    6. list = out.split("\n");
    7. list.removeLast();
    8. for (int j = 0; j < 10000; j++)
    9. x = sqrt(847239);
    10. list << QString("%1 %2").arg(i).arg(x);
    11. out = list.join("\n");
    12. commandWindow->setPlainText(out);
    13. }
    14. commandWindow->appendPlainText("done\n");
    To copy to clipboard, switch view to plain text mode 
    this doesn't display the counter but only updates the commandWIndow whenit is done, it display 99. Also if I comment out the list.removeLast();, it only shows the hunderd lines after it is finished. Suggestions are welcome.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: update a counter in PlainTextEdit on the same line

    Because for loop is disabling event dispatcher. Just write Yours code like this :
    Qt Code:
    1. ....
    2. commandWindow->setPlainText(out);
    3. QCoreApplication::processEvents();
    4. ....
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    qt_gotcha (11th August 2010)

  4. #3
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Post Re: update a counter in PlainTextEdit on the same line

    actually
    QCoreApplication::sendPostedEvents(this, 0);
    works better

    I have a QProcess (calcProcess) that is linked to a unix programme that gives of blocks of bytes (no readlines, '\n'). It outputs only on the error channel. It outputs first its name, then an umpty line, then a counter that looks like "Execute timstep xxx". That output should be processed in a PlainTextEdit called commandWindow

    my code now looks like this for those who are interested:
    Qt Code:
    1. calcProcess->setReadChannel ( QProcess::StandardError );
    2. connect(calcProcess, SIGNAL(readyReadStandardError()),this, SLOT(readFromStderr()) );
    3. connect(calcProcess, SIGNAL(finished(int)),this, SLOT(finishedModel(int)) );
    To copy to clipboard, switch view to plain text mode 

    In the starter function of the process:
    Qt Code:
    1. {
    2. [...] //get relevant proces info
    3.  
    4. commandWindow->appendPlainText(" ");
    5. commandWindow->appendPlainText(" ");
    6. commandWindow->appendPlainText(" ");
    7. xlast = commandWindow->document()->lineCount();
    8. // append extra empty lines that will be replaced with the process output
    9.  
    10. calcProcess->start(prog, args);
    11. }
    To copy to clipboard, switch view to plain text mode 
    The function to proces the output on screen is as follows:

    Qt Code:
    1. void nutshellqt::readFromStderr()
    2. {
    3. buf.clear();
    4. buf = calcProcess->readAllStandardError();
    5. onScreen(QString(buf));
    6. }
    7.  
    8. void nutshellqt::onScreen(QString buffer)
    9. {
    10. QString output;
    11. QStringList list, listb;
    12. QTimer tT;
    13. tT.setSingleShot(true);
    14.  
    15. buffer.replace('\r','\n');
    16. // replace unix CR with \n
    17. listb.clear();
    18. listb = buffer.split("\n");
    19. // split process output all out in seperate lines
    20.  
    21. output = commandWindow->toPlainText();
    22. list = output.split("\n");
    23. // get the lines in the commandWindow, might get slow?
    24.  
    25. foreach (QString str, listb)
    26. {
    27. if(str.contains("version") )
    28. list.replace(xlast-3,str);
    29. if (str.contains("Exec") )
    30. list.replace(xlast-1,str);
    31.  
    32. QCoreApplication::sendPostedEvents(this, 0);
    33. // update the plaintextedit with these two actions
    34.  
    35. tT.start(100);
    36. //delay slightly for readability
    37.  
    38. output=list.join("\n");
    39. commandWindow->setPlainText(output);
    40. // join new lines and replace the commandWindow
    41.  
    42. QTextCursor cur = commandWindow->textCursor();
    43. cur.setPosition(output.size());
    44. commandWindow->setTextCursor(cur);
    45. //put the cursor at the end
    46.  
    47. QCoreApplication::sendPostedEvents(this, 0);
    48. // update plaintextedit with last actions
    49. }
    50. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by qt_gotcha; 13th August 2010 at 11:33.

Similar Threads

  1. How to set the Text Cursor in a PlainTextEdit box?
    By Mia S. in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2010, 16:30
  2. Replies: 2
    Last Post: 13th February 2010, 18:33
  3. Autoincrement builds counter
    By iddqd in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2010, 18:45
  4. How to make plainTextEdit autofill to parent window?
    By kid2000 in forum Qt Programming
    Replies: 7
    Last Post: 17th February 2009, 09:59
  5. Counter widget
    By eu.x in forum Newbie
    Replies: 7
    Last Post: 27th February 2007, 20:30

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.