2012-08-06 63 views
7

我嘗試創建一個GUI應用程序。編程的Qt GUI設計

主窗口,QMainWindow,包含9個固定大小的標籤,也包含主窗口的大小。

我試圖讓它沒有Qt GUI Designer的編程。該項目的構建沒有錯誤,但我看不到主窗口上顯示的任何標籤和佈局。它只是空白。

這裏是我的源代碼:

WCwindow::WCwindow() 
{ 
    // initialize widgets with text 
    CAM111 = new QLabel("CAM 01"); 
    CAM121 = new QLabel("CAM 02"); 
    CAM131 = new QLabel("CAM 03"); 

    CAM211 = new QLabel("CAM 04"); 
    CAM221 = new QLabel("CAM 05"); 
    CAM231 = new QLabel("CAM 06"); 

    CAM311 = new QLabel("CAM 07"); 
    CAM321 = new QLabel("CAM 08"); 
    CAM331 = new QLabel("CAM 09"); 

    CAM111->setFixedSize(wcW,wcH); 
    CAM121->setFixedSize(wcW,wcH); 
    CAM131->setFixedSize(wcW,wcH); 
    CAM211->setFixedSize(wcW,wcH); 
    CAM221->setFixedSize(wcW,wcH); 
    CAM231->setFixedSize(wcW,wcH); 
    CAM311->setFixedSize(wcW,wcH); 
    CAM321->setFixedSize(wcW,wcH); 
    CAM331->setFixedSize(wcW,wcH); 

    QGridLayout *layout = new QGridLayout; 
    layout->addWidget(CAM111,0,0); 
    layout->addWidget(CAM121,0,1); 
    layout->addWidget(CAM131,0,2); 

    layout->addWidget(CAM211,1,0); 
    layout->addWidget(CAM221,1,1); 
    layout->addWidget(CAM231,1,2); 

    layout->addWidget(CAM311,2,0); 
    layout->addWidget(CAM321,2,1); 
    layout->addWidget(CAM331,2,2); 

    setLayout(layout); 

    setWindowTitle("Camera Window"); 
    setFixedSize(1000, 800); 

} 
當然

,類初始化並在main.cpp中誘發:

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 

    WCwindow *WCwin = new WCwindow; 

    WCwin->show(); 

    return app.exec(); 
} 

我會遇到什麼樣的bug?

+2

固定的尺寸是一個糟糕的主意,因爲系統/用戶相關字體大小,翻譯等。無論如何,wcW和wcH的值是什麼? – 2012-08-06 09:23:45

+0

順便說一句,如果您跳過標籤上的setFixedSize,它應該至少可以工作。 – 2012-08-06 09:31:57

+0

這是因爲從網絡攝像頭捕獲的圖像的大小具有固定的大小。這些標籤是拍攝圖像的持有者 – 2012-08-06 15:39:01

回答

4

下面的代碼正常工作。問題在於你沒有顯示的代碼。當您使用QMainWindow時,正如您最終承認的那樣,您需要使用您構建的新窗口小部件來設置它的centralWidget

// main.cpp 
#include <QVector> 
#include <QMainWindow> 
#include <QLabel> 
#include <QGridLayout> 
#include <QApplication> 

class WCwindow : public QMainWindow 
{ 
public: 
    WCwindow(); 
private: 
    QVector<QLabel*> cams; 
    QLabel* cam(int r, int c) const { 
     return cams[r*3 + c]; 
    } 
}; 

WCwindow::WCwindow() 
{ 
    QGridLayout *layout = new QGridLayout; 

    for (int i = 1; i < 10; ++ i) { 
     QLabel * const label = new QLabel(QString("CAM %1").arg(i, 2, 10, QLatin1Char('0'))); 
     label->setFixedSize(200, 50); 
     layout->addWidget(label, (i-1)/3, (i-1) % 3); 
     cams << label; 
    } 

    QWidget * central = new QWidget(); 
    setCentralWidget(central); 
    centralWidget()->setLayout(layout); 

    setWindowTitle("Camera Window"); 
    setFixedSize(1000, 800); 
} 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    WCwindow win; 
    win.show(); 
    return app.exec(); 
} 
+0

謝謝!我已經運行你的代碼,它工作正常。而且我也瞭解到,對於我來說,QMainWindow的情況並不一樣。我將我的類更改爲QWidget子類,並且確定 – 2012-08-06 16:19:21

+0

在「QMainWindow」中,您必須使用其「centralWidget()」。我相應地修改了代碼。 – 2012-08-06 19:14:38

1

WCwindowQMainWindow一個子類?在這種情況下,我會建議通過單擊頂部欄中的「破布局」按鈕,從窗口中刪除佈局的GUI編輯器,然後使用以下命令:

//setup all your labels and layout ... 

//creating a QWidget, and setting the WCwindow as parent 
QWidget * widget = new QWidget(this); 

//set the gridlayout for the widget 
widget->setLayout(layout); 

//setting the WCwindow's central widget 
setCentralWidget(widget); 
+0

謝謝!這是我正在尋找的答案。它打我的問題 – 2012-08-06 16:21:15

+0

你很好! – 2012-08-06 16:29:28

+0

我不能選擇2個答案? – 2012-08-07 02:52:20