2016-09-16 114 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

我只想输出都转而创建控制台窗口

可以使用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()

相关问题