2017-09-26 4 views
1

Qtを使用してQProcessに関するいくつかの問題があります。私はプッシュボタンのonClickイベントで次の関数を接続しました。基本的には、このボタンをクリックすると別のファイルを実行し、Qtプログラムでその出力を取得します。このファイルcalculatorが実行され、出力が表示され、ユーザーからの入力が待機されます。シナリオではQtで連続したQProcessのstdoutputを読む

void runPushButtonClicked() { 
    QProcess myprocess; 
    myprocess.start("./calculator") 
    myprocess.waitForFinished(); 
    QString outputData= myprocess.readStandardOutput(); 
    qDebug() << outputData; 
} 

calculatorのみいくつかの結果を出力し、最終的に終了し、そのようなファイルであるとき、これは完璧に動作します。しかし、いくつかの結果を出力してから計算機がユーザからの入力を待っている場合、私はoutputDataに何も得ません。実際には、waitForFinished()はタイムアウトしますが、waitForFinished()を削除しても、outputDataはまだ空です。

私は既にここで利用可能なソリューションのいくつかを試しましたが、このケースを処理できませんでした。どんな指導も高く評価されます。

+0

_私はすでにいくつかのソリューションを試しました。あなたが試したことを示してください。 –

+0

信号とスロットの接続による接続(プロセス、SIGNAL(readyRead())、this、SLOT(readStdOut())); – Daud

答えて

0

サブプロセスが出力を生成するときに呼び出されるシグナルハンドラをセットアップすることをお勧めします。例えば。 readyReadStandardOutputに接続する必要があります。

サブプロセスが入力を要求し、入力を送信するときに、それを識別できます。これはreadSubProcess()で行われます。以下では

#include <QtCore> 
#include "Foo.h" 

int main(int argc, char **argv) { 
    QCoreApplication app(argc, argv); 

    Foo foo; 

    qDebug() << "Starting main loop"; 
    app.exec(); 
} 

main.cppには、サブプロセスが開始され、入力がチェック。 calculatorプログラムが終了すると、メインも終了します。簡単なスクリプトを使用しているサブプロセスCalculatorの

がfoo.h

#include <QtCore> 

class Foo : public QObject { 
    Q_OBJECT 
    QProcess myprocess; 
    QString output; 

public: 
    Foo() : QObject() { 
     myprocess.start("./calculator"); 

     // probably nothing here yet 
     qDebug() << "Output right after start:" 
       << myprocess.readAllStandardOutput(); 

     // get informed when data is ready 
     connect(&myprocess, SIGNAL(readyReadStandardOutput()), 
       this, SLOT(readSubProcess())); 
    }; 

private slots: 
    // here we check what was received (called everytime new data is readable) 
    void readSubProcess(void) { 
     output.append(myprocess.readAllStandardOutput()); 
     qDebug() << "complete output: " << output; 

     // check if input is expected 
     if (output.endsWith("type\n")) { 
     qDebug() << "ready to receive input"; 

     // write something to subprocess, if the user has provided input, 
     // you need to (read it and) forward it here. 
     myprocess.write("hallo back!\n"); 
     // reset outputbuffer 
     output = ""; 
     } 

     // subprocess indicates it finished 
     if (output.endsWith("Bye!\n")) { 
     // wait for subprocess and exit 
     myprocess.waitForFinished(); 
     QCoreApplication::exit(); 
     } 
    }; 
}; 

。出力がどこで生成され、どこに入力が期待されているかを見ることができます。あなたが(例えば、GUIを表示し、他のスレッド/プロセスを....管理)あなたの主なプロセスで他に何もする必要がない場合

#/bin/bash 

echo "Sub: Im calculator!" 

# some processing here with occasionally feedback 
sleep 3 
echo "Sub: hallo" 

sleep 1 

echo "Sub: type" 
# here the script blocks until some input with '\n' at the end comes via stdin 
read BAR 

# just echo what we got from input 
echo "Sub: you typed: ${BAR}" 

sleep 1 
echo "Sub: Bye!" 

は、最も簡単にはループの後にちょうどsleepにすることですサブプロセスを作成してreadSubprocessのようなものを作成します。

+0

お時間をありがとう。 "qDebug()<<"完全出力のため、答えは私にとってはうまくいかない: "<<出力;"私の場合は決して起こらない。私は知らない。 – Daud

+0

私は起動の代わりに実行を使用すると動作しますが、コンソールの出力ではなく端末で実行します – Daud

関連する問題