2011-04-09 336 views
0

我有一個QFrame派生對象背景或邊框:QWidget的/ QFrame沒有顯示當加入到佈局

class SubjectLineDisplay : public QFrame 
{ 
    Q_OBJECT 
private: 
    // Members 

public: 
    explicit SubjectLineDisplay(const QString&, const QString&, quint32, QWidget *parent = 0); 
}; 

在構造函數中,我爲其設置一個背景和邊界:

QPalette p(palette()); 
p.setColor(QPalette::Background, QColor(255, 255, 255)); 
setPalette(p); 
setLayout(mainLayout); // The mainLayout is a VBoxLayout which is a collection of a few QLabels 
setFixedHeight(lTitle->size().height() + lId->size().height()); 

當我爲此在main()

SubjectLineDisplay* x = new SubjectLineDisplay("NETWORK", "Network Centric Programming", 4); 
x->show(); 

的小部件顯示了在一個窗口中,與背景和幀DISP正確鋪設,就像我想要的一樣。然而,當我把它添加到另一個佈局,以顯示它:

SubjectLineDisplay* lineDisplay = new SubjectLineDisplay(
      subjectNameLE->text(), idLE->text(), creditSpin->value() 
); 

emit newSubjectAdded(Course(subjectNameLE->text(), idLE->text(), creditSpin->value())); 

subjectNameLE->clear(); 
creditSpin->setValue(3); 
idLE->clear(); 

subjectLineLayout->addWidget(lineDisplay); //Adding the widget to a layout 

現在,我看不出框架或邊界。如何獲得佈局以顯示框架和邊框?我究竟做錯了什麼?

回答

2

你可以嘗試使用setAutoFillBackground(true)嗎? 據我所知,前景總是被畫出來的,但背景卻不是。

+0

非常感謝。我的背景現在顯示出來了,但我仍然看不到QFrame的任何邊框。而且,只有在將小部件添加到佈局時,我才能看到邊框。當我剛剛實例化一個QFrame派生對象的對象並調用它時,它顯示一個邊框。 – 2011-04-10 15:19:05

+0

花費的時間比我預料的要多得多! – tobilocker 2016-07-13 12:44:25