[英]QProcess::readAllStandardOutput() truncates result
My Qt program needs to send a command line with a QProcess
and retrieve the result, which will then be stored in a QString
. 我的Qt程序需要发送一个带有
QProcess
的命令行并检索结果,然后将结果存储在QString
。
Here is my code: 这是我的代码:
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QProcess p;
void sendCommand(QString command);
private slot:
void fetchResult();
// ...
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
connect(&p, SIGNAL(readyReadStandardOutput()), this, SLOT(fetchResult()));
// ...
}
void MainWindow::fetchResult()
{
QString result = p.readAllStandardOutput();
// ...
}
void MainWindow::sendCommand(QString command)
{
p.start(command);
p.waitForFinished();
}
// ...
I then send commands like this: sendCommand("cat " + filename);
然后我发送如下命令:
sendCommand("cat " + filename);
(for example) and I expect to get the result in the result
variable located in fetchResult()
. (例如)我希望得到
fetchResult()
中的result
变量的result
。
Everything works like a charm, but... if the result is too big (~700 chars) , it is trucated. 一切都像魅力一样,但......如果结果太大(约700个字符),它就会被剔除。 Strange thing: the variable contains the end of the string I am expecting.
奇怪的是:变量包含我期待的字符串的结尾 。
Where do I miss something. 我在哪里错过了什么。
As you want to wait until the end of the execution, try this, it works for me: 因为你想等到执行结束,试试这个,它对我有用:
Slot
like private slots: void cmdFinished();
private slots: void cmdFinished();
Slot
一样添加Slot
private slots: void cmdFinished();
to receive QProcess::finished
signal: QProcess::finished
信号: void MainWindow::cmdFinished()
{
// process Standard Output result
fetchResult();
// process Standard Error result
//fetchErrResult(); // add it if you want to process Errors (p.readAllStandardError())
}
QProcess::finished(int)
(remove connection to the signal readyReadStandardOutput()
): QProcess::finished(int)
(删除与信号readyReadStandardOutput()
连接): connect( &p, SIGNAL(finished(int)),
this, SLOT(cmdFinished()) );
Hope it helps you. 希望它能帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.