添加到布局时,QWidget/QFrame不显示背景或边框



我有一个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();

小部件显示在窗口中,背景和框架显示正确,正如我想要的。但是,当我将它添加到另一个布局中以显示它时:

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

现在,我看不到框架或边框。如何让布局显示框架和边框?我做错了什么?

你可以尝试使用setAutoFillBackground(true)吗?据我所知,前景总是画出来的,但背景不是。

最新更新