试图从衍生的QProcess读取stdout

时间:2015-06-19 07:08:48

标签: qt signals-slots qprocess

我已经编写了一个Qt应用程序来为后端Linux控制台应用程序创建UI。现在,我希望控制台输出和标准错误显示在我的应用程序的窗口中。

我尝试过以下代码:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

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

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QProcess *console_backend = new QProcess(this);
    QString file = "/path/to/console_executable";
    console_backend->start(file);

    console_backend->setReadChannel(QProcess::StandardOutput);

    QObject::connect(console_backend, SIGNAL(console_backend
                     ->readyReadStandardOutput()),
                     this, SLOT(this->receiveConsoleBackendOutput()));

}

void MainWindow::receiveConsoleBackendOutput()
{
    //..celebrate !
}

当我执行应用程序时,我得到的错误是:

Object::connect: No such signal
QProcess::console_backend>readyReadStandardOutput() in
../projekt_directory/mainwindow.cpp:239 
Object::connect:  (receiver name: 'MainWindow')

我是否理解信号和插槽错误?为什么没有这样的信号" ? 整个方法是错的吗?

1 个答案:

答案 0 :(得分:2)

问题在声明中

QObject::connect(console_backend, SIGNAL(console_backend
                 ->readyReadStandardOutput()),
                 this, SLOT(this->receiveConsoleBackendOutput()));

应该是

QObject::connect(console_backend, SIGNAL(readyReadStandardOutput()),
                 this, SLOT(receiveConsoleBackendOutput()));