Qt QWidget/QFrame添加到布局时不显示背景或边框
我有一个QFrame派生对象:Qt QWidget/QFrame添加到布局时不显示背景或边框,qt,Qt,我有一个QFrame派生对象: class SubjectLineDisplay : public QFrame { Q_OBJECT private: // Members public: explicit SubjectLineDisplay(const QString&, const QString&, quint32, QWidget *parent = 0); }; 在构造函数中,我为它设置了背景和边框: QPalette p(palette
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* 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)?
就我所知,前景总是画出来的,但背景不是 非常感谢。我的背景现在显示出来了,但我仍然看不到QFrame的任何边界。只有当我将小部件添加到布局中时,我才能看到边框。当我只是实例化QFrame派生对象的一个对象并在其上调用
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