我很努力为QPushButton设置背景图片。直到现在还没有成功。以下是我的代码。为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()));
}
我在样式表中使用的图像位于同一个项目文件夹中。 有人有任何解决办法吗?
在上面的代码中,我应该调整我的图像调整到适合按钮或它会自动照顾? – 2010-04-20 06:26:27
你可以去任何一个方向。最初我在编辑器中将图像大小调整为50x50,但后来决定要使用40x40 - “setIconSize()”将图标缩放为任何你想要的。 – 2010-04-20 14:21:15
这是不正确的,你不必设置平面属性或自动填充背景。你可以简单地通过'setIcon(“:/ path/to/image.png”)加载图像。我通常会首先将图像加载到一个'QPixmap'中,以便它可以作为绘制设备使用,然后将Pixmap传递给任何需要它的元素。 – 2014-04-28 18:48:13