Qt readAllStandardOutput()导致程序意外结束

时间:2011-06-04 23:57:20

标签: qt qt4

当我尝试向QString发出readAllStandardOutput()时,即使我将QByteStream转换为QString,也不知道为什么会这样?继承人来源

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
#include <QString>


#include "exeprocess.h"

/*main window ---------------------------------------*/

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

signals:
    void outLog(QString outLogVar); //plug this into the QTextEdit box

public slots:
    void logReady(); // plug the QProcess into this


private:
    Ui::MainWindow *ui;
};




#endif // MAINWINDOW_H

mainwindow.cpp

#include <QByteArray>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QProcess *proc = new QProcess; //initialize proc
    QStringList arguments;
    arguments << "-h";

    connect(proc, SIGNAL(readyReadStandardOutput ()), this, SLOT(logReady()));

    proc->start("/Applications/Graphics/3Delight-9.0.87/bin/renderdl", arguments);

}

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

void MainWindow::logReady(){
    QString str = proc->readAllStandardOutput();
    emit outLog(str);
}

谢谢!

1 个答案:

答案 0 :(得分:5)

这一行是问题所在:

QProcess *proc = new QProcess; //initialize proc

您通过重用该名称来隐藏成员变量。调用logReady时,调用readAllStandardOutput()的'proc'是一个不同的(null)指针,所以一切都崩溃了。修复很简单:用

替换上面的行
proc = new QProcess;