Problem processing output from QProcess
-
I tried redirecting the MKVToolNix output to a file, and the full output appears including all Unicode characters.
You now have a couple of things you can play with, to discover where your actual problem lies:
cat
the file in a terminal. Do all the characters display correctly?- Change your
QProcess
command tocat
that file. Do you get the output bytes back correctly or not? This tells you whether it's running the MKVToolNix sub-process or whether it's the content of the output which is problematic. - Compare the output bytes in the file against what you see in the
readyReadStandardOutput
(as far as it goes before getting cut off). Are they identical or is there a difference?
@JonB said in Problem processing output from QProcess:
You now have a couple of things you can play with, to discover where your actual problem lies:
cat
the file in a terminal. Do all the characters display correctly?
Yes, the full output is displayed along with the Unicode characters.
- Change your
QProcess
command tocat
that file. Do you get the output bytes back correctly or not? This tells you whether it's running the MKVToolNix sub-process or whether it's the content of the output which is problematic.
Yes, the full contents of the file is returned by
readAllStandardOutput()
, including the Unicode characters.- Compare the output bytes in the file against what you see in the
readyReadStandardOutput
(as far as it goes before getting cut off). Are they identical or is there a difference?
I tried the below, where m_file is the contents of the file in a QByteArray. It came out as identical.
void IUIInfoDisplay::OutputText() { QByteArray output = m_qprocMKVToolNix.readAllStandardOutput(); bool identical = true; for (int x = 0 ; x < output.size() ; ++x) { if (output.at(x) != m_file.at(x)) { QMessageBox::information(this, "Different", "Different", QMessageBox::Ok); identical = false; } } if (identical) QMessageBox::information(this, "Identical", "Identical", QMessageBox::Ok); }
I also tried it on Linux (Ubuntu) and like Windows there are no problems reading the output from MKVToolNix.
I think I'll try asking the author of MKVToolNix whether he thinks it's a Qt issue or an MKVToolNix issue. I can easily work around it by sending the output to a temporary file and reading it from there, but it would be nice to know what the cause is.
-
Probably useless suggestion but did you try using
QTextStream
instead of just accessing the buffer directly?void IUIInfoDisplay::OutputText() { qProcess.setReadChannel(QProcess::StandardOutput); QTextStream reader(&qProcess); QString line; while (reader.readLineInto(&line)) qTextEdit->insertPlainText(line +'\n'); }
-
@JonB said in Problem processing output from QProcess:
You now have a couple of things you can play with, to discover where your actual problem lies:
cat
the file in a terminal. Do all the characters display correctly?
Yes, the full output is displayed along with the Unicode characters.
- Change your
QProcess
command tocat
that file. Do you get the output bytes back correctly or not? This tells you whether it's running the MKVToolNix sub-process or whether it's the content of the output which is problematic.
Yes, the full contents of the file is returned by
readAllStandardOutput()
, including the Unicode characters.- Compare the output bytes in the file against what you see in the
readyReadStandardOutput
(as far as it goes before getting cut off). Are they identical or is there a difference?
I tried the below, where m_file is the contents of the file in a QByteArray. It came out as identical.
void IUIInfoDisplay::OutputText() { QByteArray output = m_qprocMKVToolNix.readAllStandardOutput(); bool identical = true; for (int x = 0 ; x < output.size() ; ++x) { if (output.at(x) != m_file.at(x)) { QMessageBox::information(this, "Different", "Different", QMessageBox::Ok); identical = false; } } if (identical) QMessageBox::information(this, "Identical", "Identical", QMessageBox::Ok); }
I also tried it on Linux (Ubuntu) and like Windows there are no problems reading the output from MKVToolNix.
I think I'll try asking the author of MKVToolNix whether he thinks it's a Qt issue or an MKVToolNix issue. I can easily work around it by sending the output to a temporary file and reading it from there, but it would be nice to know what the cause is.
Change your QProcess command to cat that file. Do you get the output bytes back correctly or not? This tells you whether it's running the MKVToolNix sub-process or whether it's the content of the output which is problematic.
Yes, the full contents of the file is returned by readAllStandardOutput(), including the Unicode characters.
From what you have said then, since you can read all the exact same characters from full without problem but not from running MKVToolNix, under MacOS only, it appears there is a problem running/reading that from
QProcess
under MacOS. Which seems a little surprising, but there you are...Everything points to a "buffering" problem, where you simply do not receive a bunch of further characters from the process after the first block. (When redirected to file, all the characters end up there on file close/termination.) Are you sure you are doing the full ready reads followed by the normal "finished" signal? If I were you, in the "finished" signal I would do an extra "read all output" --- I don't seem to need it under Linux/Windows, but maybe just possibly under MacOS you fail to get the final "ready read" before the "finished". Or, if you do not need to use the signal, there is some other function for "read all output" after the sub-process has finished. This really ought to be the problem...!
-
Probably useless suggestion but did you try using
QTextStream
instead of just accessing the buffer directly?void IUIInfoDisplay::OutputText() { qProcess.setReadChannel(QProcess::StandardOutput); QTextStream reader(&qProcess); QString line; while (reader.readLineInto(&line)) qTextEdit->insertPlainText(line +'\n'); }
@VRonin said in Problem processing output from QProcess:
qProcess.setReadChannel(QProcess::StandardOutput);
QTextStream reader(&qProcess);
QString line;
while (reader.readLineInto(&line))
qTextEdit->insertPlainText(line +'\n');I gave it a try, but the output still terminates when it reaches the first Unicode character.
Thanks anyway for the suggestion.
-
@VRonin said in Problem processing output from QProcess:
qProcess.setReadChannel(QProcess::StandardOutput);
QTextStream reader(&qProcess);
QString line;
while (reader.readLineInto(&line))
qTextEdit->insertPlainText(line +'\n');I gave it a try, but the output still terminates when it reaches the first Unicode character.
Thanks anyway for the suggestion.
-
Change your QProcess command to cat that file. Do you get the output bytes back correctly or not? This tells you whether it's running the MKVToolNix sub-process or whether it's the content of the output which is problematic.
Yes, the full contents of the file is returned by readAllStandardOutput(), including the Unicode characters.
From what you have said then, since you can read all the exact same characters from full without problem but not from running MKVToolNix, under MacOS only, it appears there is a problem running/reading that from
QProcess
under MacOS. Which seems a little surprising, but there you are...Everything points to a "buffering" problem, where you simply do not receive a bunch of further characters from the process after the first block. (When redirected to file, all the characters end up there on file close/termination.) Are you sure you are doing the full ready reads followed by the normal "finished" signal? If I were you, in the "finished" signal I would do an extra "read all output" --- I don't seem to need it under Linux/Windows, but maybe just possibly under MacOS you fail to get the final "ready read" before the "finished". Or, if you do not need to use the signal, there is some other function for "read all output" after the sub-process has finished. This really ought to be the problem...!
@JonB said in Problem processing output from QProcess:
From what you have said then, since you can read all the exact same characters from full without problem but not from running MKVToolNix, under MacOS only, it appears there is a problem running/reading that from
QProcess
under MacOS. Which seems a little surprising, but there you are...Everything points to a "buffering" problem, where you simply do not receive a bunch of further characters from the process after the first block. (When redirected to file, all the characters end up there on file close/termination.) Are you sure you are doing the full ready reads followed by the normal "finished" signal?
Pretty much certain. If there are no Unicode characters in the ouptut it will read the full output without issues, even when it's hundreds of thousand of lines.
If I were you, in the "finished" signal I would do an extra "read all output" --- I don't seem to need it under Linux/Windows, but maybe just possibly under MacOS you fail to get the final "ready read" before the "finished". Or, if you do not need to use the signal, there is some other function for "read all output" after the sub-process has finished. This really ought to be the problem...!
I tried adding another
readAllStandardOutput()
in the Finished() function. It returns a QByteArray with a size of 0, so there is no further output.