Cannot read output using readyReadStandardOutput in Qt

好久不见. 提交于 2019-12-23 01:34:07

问题


I am using Qt and I am unable to get output of a exe file using readyReadStandardOutput.

Here is my code.

mainwindow.cpp

void MainWindow::on_pushButton_24_clicked()
{
    myprocess = new QProcess(this);
    myprocess->start("files\\helloworld.exe");
    connect(myprocess, SIGNAL(readyReadStandardOutput ()), this, SLOT(outlog()));
}

void MainWindow::outlog()
{
    QString abc;

    abc = myprocess->readAllStandardOutput();

    emit outlogtext(abc);
    ui->lineEdit_4->setText(abc);
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QProcess *myprocess;

signals:
    void outlogtext(QString ver);

private slots:
    void outlog();
private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

The helloworld.exe will just give a output "Hello world" but I cannot see it in the textEdit, whats wrong with my code? I am very new to Qt. Thank you


回答1:


I got the program working. Below is the code.

mainwindow.hpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QtGui>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void outlogtext(QString ver);

private slots:
    void outlog();
    void on_pushButton_24_clicked();

private:
    QPushButton* pushButton_24;
    QLineEdit* lineEdit_4;
    QProcess *myprocess;
};

#endif // MAINWINDOW_HPP

main.cpp

#include <QtCore>
#include <QtGui>
#include <QDebug>
#include "mainwindow.hpp"

MainWindow::MainWindow(QWidget* parent)
  : QMainWindow(parent)
{
    pushButton_24 = new QPushButton;
    connect(pushButton_24, SIGNAL(clicked()),
            this, SLOT(on_pushButton_24_clicked()));

    lineEdit_4 = new QLineEdit;

    QWidget* central = new QWidget;
    QLayout* layout = new QVBoxLayout();
    layout->addWidget(pushButton_24);
    layout->addWidget(lineEdit_4);
    central->setLayout(layout);
    setCentralWidget(central);
}

MainWindow::~MainWindow()
{
}

void MainWindow::on_pushButton_24_clicked()
{
    myprocess = new QProcess(this);
    connect(myprocess, SIGNAL(readyReadStandardOutput()),
            this, SLOT(outlog()));
    myprocess->start("./helloworld.exe");

    // For debugging: Wait until the process has finished.
    myprocess->waitForFinished();
    qDebug() << "myprocess error code:" << myprocess->error();
}

void MainWindow::outlog()
{
    QString abc = myprocess->readAllStandardOutput();
    emit outlogtext(abc);
    lineEdit_4->setText(abc);
}

int main(int argc, char* argv[])
{
   QApplication app(argc, argv);
   MainWindow win;
   win.show();
   return app.exec();
}

helloworld.cpp

#include <iostream>
int main()
{
  std::cout << "Hello World!" << std::endl;
}

Some things I changed:

  • After constructing an object, I always connect signals and slots before performing the actual operation on the object, which might be calling show() for widgets or calling start() for threads. So I can be sure that I do not miss a signal like started(), for example.

  • I ran the program on Linux. There I had to make sure that helloworld.exe was on my path and I changed the command to ./helloworld.exe. I didn't create the sub-directory called files as in your example.

  • The character to separate directories in Qt is the slash /. There are special functions to convert between the Qt-style and the native-style, when you want to display something to the user. Internally always make use of slashes. This even works for Windows programs (many console commands can cope with a slash instead of a backslash, too).

  • Adding debug output is really, really valuable during development. If the Makefile is not set up correctly or something breaks, the helloworld.exe might end up in a directory, where it is not expected. Thus, I added code to wait for some time until the process has finished. This does not hurt because helloworld.exe needs just some milliseconds to run. Afterwards, I print the error code of QProcess just to be sure that the program has been found and could be executed. So I can be sure that the executable is on my path, that the executable flag is set, that I have permissions for executing the file etc.

I don't know exactly what causes the problem on your machine. However, comparing your solution to mine, having a look at the error code of QProcess and setting break points inside the slots should help you finding the error.



来源:https://stackoverflow.com/questions/11921004/cannot-read-output-using-readyreadstandardoutput-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!