版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010168781/article/details/88251213
1、问题描述
在QtCreator练习QStylePlugin的例子时,没有效果,原因是QPalette使用不当造成。
详见:https://blog.csdn.net/u010168781/article/details/88250451
2、解决方法
解决方法很简单,我们只是为了演示QStylePlugin的效果,然而QPushButton不能通过QPalette来改变样式,因此使用QLabel来做演示,代码修改如下:
1)stylewindow.cpp文件中将QPushButton换成QLabel
StyleWindow::StyleWindow()
{
QLabel *styledLabel = new QLabel(tr("Red Text"));
QGridLayout *layout = new QGridLayout;
layout->addWidget(styledLabel);
QGroupBox *styleBox = new QGroupBox(tr("A simple style QLabel"));
styleBox->setLayout(layout);
QGridLayout *outerLayout = new QGridLayout;
outerLayout->addWidget(styleBox, 0, 0);
setLayout(outerLayout);
setWindowTitle(tr("Style Plugin Example"));
}
2)simplestyle.cpp文件中将QPalette::Button改为QPalette::WindowText,最好添加打印看看插件有没有调用。
void SimpleStyle::polish(QPalette &palette)
{
qDebug("plugin test");
palette.setColor(QPalette::WindowText, Qt::red);
}
效果图如下