2012-08-12 35 views
1

讀取使用readyReadStandardOutput輸出我使用Qt和我無法得到使用readyReadStandardOutput一個exe文件的輸出。不能在Qt的

這是我的代碼。

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 

的將HelloWorld.exe的只是提供一個輸出 「Hello World」 的,但我不能看到它的文本編輯,我的代碼有什麼問題?我對Qt非常陌生。謝謝

+0

你確定outlog()是插槽,而不僅僅是一個函數嗎? – Blood 2012-08-12 09:29:54

+0

我想是的,我在第一篇文章中也添加了mainwindow.h。請看一看。 – 2012-08-12 09:41:08

+0

您應該追加文本,而不是替換它,因爲輸出可能會出現在多個塊中,甚至可能會發出額外的readyRead以指示管道關閉。 – 2012-08-12 10:00:16

回答

3

我得到了程序的工作。以下是代碼。

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

有些事情我改變:

  • 構建一個對象後,我總是對象,這可能對小部件被調用show()上執行 實際操作或調用 start()線程之前連接信號和槽。所以我可以肯定,我不會錯過像started(), 等信號。

  • 我跑在Linux上的程序。在那裏,我必須確保helloworld.exe是我 路徑上,我改變了命令./helloworld.exe。我沒有創建子目錄 ,名稱爲files,如您的示例中所示。

  • 在Qt的單獨目錄中字符是斜線/。當你想向用戶顯示某些東西時,有特殊的功能可以在Qt風格和本機風格之間進行轉換。內部始終使用斜槓。這甚至適用於Windows程序(許多控制檯命令也可以用斜槓代替反斜槓)。

  • 增加調試輸出是真的,在開發過程中真正有價值的。如果生成文件是 未正確設置或發生中斷,則helloworld.exe可能會以不在預期的目錄結束。因此,我添加了代碼以等待一段時間,直到該過程結束。這不會傷害,因爲helloworld.exe需要幾毫秒才能運行。之後,我打印出QProcess的錯誤代碼,以確保該程序已被找到並可以執行。所以我可以確定可執行文件在我的路徑上,可執行標誌被設置,我有權執行文件等。

我不確切地知道是什麼原因導致您的機器上的問題。但是,將您的解決方案與我的解決方案進行比較,查看QProcess的錯誤代碼並在插槽內設置斷點,可以幫助您找到錯誤。

+0

非常感謝! 我得到它的工作,實際上我的exe有問題,我想出了使用qdebug,如你所示。非常感謝! – 2012-08-13 15:03:12