设置QPushButton的背景图像

Sur*_*dhi 6 background image qt4

我正在努力为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()));
}
Run Code Online (Sandbox Code Playgroud)

我在样式表中使用的图像位于同一个项目文件夹中.有人有任何解决方案吗?

Bri*_*ach 6

您必须将flat属性设置为true:

button1->setFlat(true);

您还必须设置autofillbackground -

button1->setAutoFillBackground(true);

你可能想看看QToolButton,它不需要它是平的,以便渲染图像.我正在我正在写的应用程序中使用它们,它们看起来非常好看:

m_showAddCommentButton = new QToolButton();
m_showAddCommentButton->setAutoFillBackground(true);
palette = m_showAddCommentButton->palette();
palette.setColor(QPalette::Button,QColor(82,110,166));
m_showAddCommentButton->setPalette(palette);
m_showAddCommentButton->setIcon(QIcon(":/uiImages/addComment_50_50.jpg"));
m_showAddCommentButton->setIconSize(QSize(40,40));
m_showAddCommentButton->setToolTip("Comment");
connect(m_showAddCommentButton, SIGNAL(clicked()),
        manager, SLOT(showAddComment()));
hLayout->addWidget(m_showAddCommentButton,0);
Run Code Online (Sandbox Code Playgroud)

(我的图像存储为资源)