2016-09-16 9 views
-1

下記のウィンドウでデータを表示するにはどうしたらいいですか?これはコンソールアプリケーションで、すべて正常に動作します。私は出力をコンソールの代わりに作成されたウィンドウに入れたいだけです。コンソールの代わりにQLabelにプログラム出力をリダイレクト

#include <QApplication> 
#include <QLabel> 
#include <QString> 
#include "Prob3TableInherited.h" 

int main(int argc, char *argv[]) { 
    cout << "Entering problem number 3" << endl; 
    int rows = 5; 
    int cols = 6; 

    Prob3TableInherited tab("Problem3.txt", rows, cols); 
    const int *naugT = tab.getTable(); 

    for (int i = 0; i < rows; i++) 
    { 
     for (int j = 0; j < cols; j++) 
     { 
      cout << naugT[i*cols + j] << " "; 
     } 
     cout << endl; 
    } 
    cout << endl; 

    const int *augT = tab.getAugTable(); 
    for (int i = 0; i <= rows; i++) 
    { 
     for (int j = 0; j <= cols; j++) 
     { 
      cout << augT[i*(cols + 1) + j] << " "; 
     } 
     cout << endl; 
    } 

    // How can I pass the data? 
    QString data("Need To Pass Data Here"); 

    //Create the Window Application 
    QApplication a(argc, argv); 
    QLabel *label=new QLabel(data); 

    //Make it visible 
    label->show(); 

    return a.exec(); 
} 

答えて

0

QApplicationにラベルを添付する必要があります。 この答えを見てみましょう:

Adding a label to a widget

+0

あなたはQtウィンドウ内に表示されるようにラベルを取得できましたか? – Jose

0

私はちょうどあなたがQStringListを使用して、それに出力を追加することができます代わりに、コンソール

の出力はウィンドウ作成になりたいですcoutの代わりに。たとえば、あなたの最初のループは

... 
QStringList outputList; 
for (int i = 0; i < rows; i++) 
{ 
    for (int j = 0; j < cols; j++) 
    { 
     outputList << naugT[i*cols + j] << " "; 
    } 
    outputList << "\n"; 
} 
outputList << "\n"; 
... 

のようになります

QLabel * label = new QLabel(outputList.join("")); 

を次のように、あなたは

QString data = outputList.join(""); 
QLabel * label = new QLabel(data); 
+0

返信いただきありがとうございますが、動作しませんでした。 – equati0n

+0

働いていないとはどういう意味ですか? – HazemGomaa

+0

私はあなたが言ったことをして、コードはコンパイルされませんでした。 – equati0n

0

と同じであるあなたのラベルにリストを追加することができウィジェットを作成QWidget *w = new QWidget()彼にラベルを添付してください。

QLabel *label = new QLabel(w); 
QHBoxLayout *layout = new QHBoxLayout(); 
label->setText("your data"); 
layout->addWidget(label); 
setLayout(layout); 

ウィジェットを表示するよりもw->show()

関連する問題