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

时间:2011-04-09 20:04:04

标签: qt

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

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

1 个答案:

答案 0 :(得分:2)

您可以尝试使用setAutoFillBackground(true)吗? 据我所知,前景总是被绘制,但背景却不是。

相关问题