поймать sytem ("cls") дочерним QProcess - PullRequest
0 голосов
/ 16 января 2019

Я пытаюсь захватить текстовый вывод и ввод дочернего QProcess, теперь мне нужно, чтобы всякий раз, когда дочерняя система использовала систему ("cls"), я получала уведомление, чтобы я могла очистить свой QTextEdit для отображения и получения дочерних выходных данных
editProcess.h

#ifndef EDITPROCESS_H
#define EDITPROCESS_H
#include <QPointer>
#include <QProcess>
#include <memory>

class QPlainTextEdit;

// a Process with plainTextEdit as output and input window
class EditProcess : public QProcess {
  Q_OBJECT
public:
  explicit EditProcess(QWidget *parent = nullptr);
  QPointer<QPlainTextEdit> edit() { return m_textEdit; }
  ~EditProcess();
signals:

public slots:

private:
  QPointer<QPlainTextEdit> m_textEdit;

protected:
  bool eventFilter(QObject *, QEvent *event) override;
};

#endif // EDITPROCESS_H

editProcess.cpp

#include "editprocess.h"
#include <QDebug>
#include <QKeyEvent>
#include <QPlainTextEdit>

EditProcess::EditProcess(QWidget *parent)
    : QProcess(parent), m_textEdit{new QPlainTextEdit}
// zm_textEdit(std::make_shared<QPlainTextEdit>(parent))
{
  QObject::connect(this, &EditProcess::readyReadStandardOutput, [this]() {
    m_textEdit->insertPlainText(readAllStandardOutput());
  });

  QObject::connect(this, &EditProcess::readyReadStandardError, [this]() {
    m_textEdit->insertPlainText(readAllStandardError());
  });

  QObject::connect(
      this,
      static_cast<void (EditProcess::*)(int, QProcess::ExitStatus)>(
          &EditProcess::finished),
      [this](int exitCode, QProcess::ExitStatus) -> void {
        m_textEdit->insertPlainText(readAllStandardOutput());
        m_textEdit->insertPlainText(
            QString("\nProgram Finished with exit code: %1").arg(exitCode));
      });

  m_textEdit->installEventFilter(this);
}

EditProcess::~EditProcess() { kill(); }

bool EditProcess::eventFilter(QObject *, QEvent *event) {
  if (event->type() == QEvent::KeyPress) {
    auto keyEvent = dynamic_cast<QKeyEvent *>(event);
    if (keyEvent->modifiers() == Qt::CTRL && keyEvent->key() == Qt::Key_C)
      kill();
    else
      write(keyEvent->text().toUtf8());
  }
  return false;
}

mainwidow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);

  auto p = new EditProcess(parent);
  p->edit()->setParent(centralWidget());
  p->setProgram(
      "test.exe");
  p->start();
  qDebug() << p->errorString();
}  

если я сейчас вызываю system("cls") из дочернего процесса, ничего не происходит, QTextEdit остается тем же, я не заметил никаких изменений

...