2011-06-04 180 views
0

當我試圖發射readAllStandardOutput()到一個QString即時獲取意外的程序崩潰,即使我將QByteStream轉換爲QString,任何想法,爲什麼?繼承人德源Qt readAllStandardOutput()導致意外的程序結束

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); 
} 

謝謝!

回答

5

這條線的問題是:

QProcess *proc = new QProcess; //initialize proc 

你通過重複使用該名稱的陰影成員變量。當調用logReady時,你調用readAllStandardOutput()的'proc'是一個不同的(空)指針,所以一切都崩潰了。修復很簡單:用上述行代替

proc = new QProcess; 
+0

這是一個夢想,但我不知道爲什麼,你是什麼意思的影子?謝謝PAG,j – 2011-06-05 00:09:14

+0

@jonathan當你聲明本地的'proc'變量時,它只是隱藏了類成員變量的decalration,所以方法中'proc'的所有用法引用本地的,不是成員變量。如果你至少有最小的C++知識,你應該看到錯誤,知道它被指出。 – 2011-06-05 00:15:28

+1

通過使用'new QProcess(this)'使主窗口成爲進程的父進程也是一個好主意,這樣當主窗口get被銷燬時它會被自動刪除。 – 2011-06-05 00:16:49