自定义一个Widget,内包含两个Label。
想要设置Widget整体的背景色
目标图如附件sample2.jpg
实际执行效果如附件图sample.jpg
仅仅设置了内部包含的两个Label的背景色。
构造函数代码如下:
DisplayInfoArea::DisplayInfoArea(QWidget *parent)
: QWidget(parent),
label1(0),
label2(0)
{
this->setAutoFillBackground(true); // 这行加或不加没有明显影响。
QVBoxLayout *mainLayout = new QVBoxLayout(this);
label1 = new QLabel(str1, this);
label1->setGeometry(10, 10, 200, 80);
QFont myFont("Times", 12, QFont::Light, FALSE);
label1->setFont(myFont);
label2 = new QLabel(str2, this);
label2->setGeometry(10, 100, 200, 80);
label2->setFont(myFont);
mainLayout->addWidget(label1);
mainLayout->addWidget(label2);
setLayout(mainLayout);
}
函数调用代码如下:
void SettingWindow::initDisplayArea()
{
QLabel *AccountTitle = new QLabel(tr("Account"), this);
AccountTitle->setGeometry(20, 0, 100, 20);
m_displayAccountInfoArea = new DisplayInfoArea(this);
m_displayAccountInfoArea->setGeometry(20, 20, 300, 160);
QString str(tr("424242424"));
m_displayAccountInfoArea->updateInfo(str, str);
QLabel *PhoneTitle = new QLabel(tr("Phone"), this);
PhoneTitle->setGeometry(20, 190, 100, 20);
// 以下是调用自定义的窗口代码:
m_displayPhoneInfoArea = new DisplayInfoArea(this);
m_displayPhoneInfoArea->setGeometry(20, 220, 300, 160);
m_displayPhoneInfoArea->updateInfo(str, str);
const QString str3 = QString("background - color: #FFFFBB;" "border - radius: 5px;");
m_displayAccountInfoArea->setStyleSheet("background-color: #202020;");
}
[ 此帖被jiangz3在2010-04-14 09:49重新编辑 ]