简体   繁体   English

QProcess :: readAllStandardOutput()截断结果

[英]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: 这是我的代码:

MainWindow.h MainWindow.h

class MainWindow : public QMainWindow
{
  Q_OBJECT

private:
  QProcess p;

  void sendCommand(QString command);

private slot:
  void fetchResult();

  // ...
}

MainWindow.cpp MainWindow.cpp

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: 因为你想等到执行结束,试试这个,它对我有用:

  • add 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())
}
  • connect only 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM