2011-04-09 334 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