我正在努力为QPushButton设置背景图像。到现在为止没有成功。下面是我的代码
appsWidget::appsWidget(QWidget *parent)
:QWidget(parent)
{
QPushButton *button1 = new QPushButton("SETTINGS",this);
QPushButton *button2 = new QPushButton("TEST",this);
QPushButton *button3 = new QPushButton("IE",this);
button1->setStyleSheet("background-image:url(config.png)"); -> No success
qDebug("appWidget initialized.");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
this->setLayout(layout);
connect(button1,SIGNAL(clicked()),this,SLOT(setClickIndex1()));
connect(button2,SIGNAL(clicked()),this,SLOT(setClickIndex2()));
connect(button3,SIGNAL(clicked()),this,SLOT(setClickIndex3()));
}
我在样式表中使用的图像位于同一个项目文件夹中。 有人有办法吗
您可以使用画笔作为调色板元素来填充任何小部件的背景,例如当按钮为平面时工作的QPushButton
必须将“平面”属性设置为true:
button1->setFlat(true);
您还必须设置自动填充背景-
button1->setAutoFillBackground(true);
您可能希望查看QToolButton,它不需要为渲染图像而使其平坦。我正在写一个应用程序中使用它们,它们看起来非常漂亮:
(我的图像存储为资源)
您的css选择器不正确
你应该这样做:
相关问题 更多 >
编程相关推荐