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:
calcProcess
->setReadChannel
( QProcess::StandardError );
connect(calcProcess, SIGNAL(readyReadStandardError()),this, SLOT(readFromStderr()) );
connect(calcProcess, SIGNAL(finished(int)),this, SLOT(finishedModel(int)) );
calcProcess->setReadChannel ( QProcess::StandardError );
connect(calcProcess, SIGNAL(readyReadStandardError()),this, SLOT(readFromStderr()) );
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:
{
[...] //get relevant proces info
commandWindow->appendPlainText(" ");
commandWindow->appendPlainText(" ");
commandWindow->appendPlainText(" ");
xlast = commandWindow->document()->lineCount();
// append extra empty lines that will be replaced with the process output
calcProcess->start(prog, args);
}
{
[...] //get relevant proces info
commandWindow->appendPlainText(" ");
commandWindow->appendPlainText(" ");
commandWindow->appendPlainText(" ");
xlast = commandWindow->document()->lineCount();
// append extra empty lines that will be replaced with the process output
calcProcess->start(prog, args);
}
To copy to clipboard, switch view to plain text mode
The function to proces the output on screen is as follows:
void nutshellqt::readFromStderr()
{
buf.clear();
buf = calcProcess->readAllStandardError();
}
void nutshellqt
::onScreen(QString buffer
) {
tT.setSingleShot(true);
buffer.replace('\r','\n');
// replace unix CR with \n
listb.clear();
listb = buffer.split("\n");
// split process output all out in seperate lines
output = commandWindow->toPlainText();
list = output.split("\n");
// get the lines in the commandWindow, might get slow?
{
if(str.contains("version") )
list.replace(xlast-3,str);
if (str.contains("Exec") )
list.replace(xlast-1,str);
// update the plaintextedit with these two actions
tT.start(100);
//delay slightly for readability
output=list.join("\n");
commandWindow->setPlainText(output);
// join new lines and replace the commandWindow
cur.setPosition(output.size());
commandWindow->setTextCursor(cur);
//put the cursor at the end
// update plaintextedit with last actions
}
}
void nutshellqt::readFromStderr()
{
QByteArray buf;
buf.clear();
buf = calcProcess->readAllStandardError();
onScreen(QString(buf));
}
void nutshellqt::onScreen(QString buffer)
{
QString output;
QStringList list, listb;
QTimer tT;
tT.setSingleShot(true);
buffer.replace('\r','\n');
// replace unix CR with \n
listb.clear();
listb = buffer.split("\n");
// split process output all out in seperate lines
output = commandWindow->toPlainText();
list = output.split("\n");
// get the lines in the commandWindow, might get slow?
foreach (QString str, listb)
{
if(str.contains("version") )
list.replace(xlast-3,str);
if (str.contains("Exec") )
list.replace(xlast-1,str);
QCoreApplication::sendPostedEvents(this, 0);
// update the plaintextedit with these two actions
tT.start(100);
//delay slightly for readability
output=list.join("\n");
commandWindow->setPlainText(output);
// join new lines and replace the commandWindow
QTextCursor cur = commandWindow->textCursor();
cur.setPosition(output.size());
commandWindow->setTextCursor(cur);
//put the cursor at the end
QCoreApplication::sendPostedEvents(this, 0);
// update plaintextedit with last actions
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks