我正在努力为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()));
}
我在样式表中使用的图像位于同一项目文件夹中。 有人有解决方案吗?
setIconSize()
函数可按照需求缩放图标。 - Brian RoachsetIcon(":/path/to/image.png")
加载图像即可。我通常会先将图像加载到QPixmap
中,以便将其用作绘画设备,然后将Pixmap传递给需要它的任何元素。 - Chef Pharaoh