2010-09-14 49 views
0

我想這樣爲什麼我的小部件不顯示?

enter image description here

在QT我創建的小部件,並把在主窗口部件創建佈局,問題是不可見的。沒有顯示任何部件。請幫我解決這個問題

完整的源代碼 sandbox.ifuturemec.com/test.zip

的源代碼 mainWindow.cpp

#include "mainwindow.h" 
#include <QtGui> 
#include "headerbar.h" 
#include <QGridLayout> 
#include <QPushButton> 
#include <QBoxLayout> 
#include "statusbar.h" 
#include "leftpanel.h" 
#include "rightpanel.h" 
#include "centerpanel.h" 

mainWindow::mainWindow(QWidget *parent) : QWidget(parent) 
{ 
    QGridLayout *layout=new QGridLayout(this); 

    headerBar *Header= new headerBar(this); 
    leftPanel *LeftPanel=new leftPanel(this); 
    centerPanel *CenterPanel=new centerPanel(this); 
    rightPanel *RightPanel=new rightPanel(this); 
    statusBar *Status=new statusBar(this); 

    QHBoxLayout *box=new QHBoxLayout(); 
    box->addWidget(LeftPanel); 
    box->addWidget(CenterPanel); 
    box->addWidget(RightPanel); 

    layout->addWidget(Header,0,0); 
    layout->addLayout(box,1,0); 
    layout->addWidget(Status,2,0); 

    setLayout(layout); 
} 

mainWindow::~mainWindow() {} 

mainWindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QWidget> 

class mainWindow : public QWidget 
{ 
    Q_OBJECT 
public: 
    mainWindow(QWidget *parent = 0); 
    ~mainWindow(); 

signals: 

public slots: 

}; 

#endif // MAINWINDOW_H 

headerbar.cpp

#include "headerbar.h" 
#include <QPushButton> 
#include <QMessageBox> 

headerBar::headerBar(QWidget *parent) : QWidget(parent) 
{ 
    this->setMaximumHeight(24); 
    this->setStyleSheet("background-color: rgb(85, 170, 255)"); 
} 

headerBar::~headerBar(){} 

headerbar.h

#ifndef HEADERBAR_H 
#define HEADERBAR_H 

#include <QWidget> 

class headerBar : public QWidget 
{ 
    Q_OBJECT 
public: 
    headerBar(QWidget *parent = 0); 
    ~headerBar(); 

signals: 

public slots: 

}; 

#endif // HEADERBAR_H 

請幫我解決這個問題。

回答

4

其實,你的小工具 show!但他們是空的,沒有什麼可顯示的!

關於您設置的背景色:background-color屬性未顯示,因爲QWidget不支持此屬性。

看看有關這方面的文檔:List of stylable widgets

更具體地:

QWidget的:僅支持的背景下,背景剪輯和背景來源的性質。

如果你試圖把,例如,在你的widget標籤,你會看到他們這樣做顯示:

centerPanel::centerPanel(QWidget *parent) : 
    QWidget(parent) 
{ 
    QHBoxLayout *box = new QHBoxLayout(this); 
    QLabel* pLabel = new QLabel("Center panel", this); 
    box->addWidget(pLabel); 
    this->setStyleSheet("background-color: rgb(85, 100, 100)"); 
} 

alt text

+0

謝謝。它正在工作。但我需要解決這個佈局,出小部件也應該全屏顯示。請提出一些建議。 – saravanan 2010-09-14 06:21:01

0

如果你只是想在mainWindow有可能會嘗試忘記使用樣式表並重寫paintEvent方法,如下所示:

void mainWindow::paintEvent(QPaintEvent *event) 
{ 
setPalette(QPalette(QColor(85, 170, 255))); 
setAutoFillBackground(true); 
} 
+0

我需要將圖像作爲背景添加到窗口小部件。我不想要純色。我是QT新手。請提出一些建議。我需要在運行時創建圖像並將該圖像作爲背景。 – saravanan 2010-09-14 07:34:33

+0

'QFrame'可能是您正在尋找的東西,至少它支持background-image屬性:http://doc.trolltech.com/4.6/stylesheet-examples.html#customizing-qframe – teukkam 2010-09-14 08:06:25