设置背景图片QPushButton [英] Setting background image for QPushButton

查看:1286
本文介绍了设置背景图片QPushButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我奋力设置一个背景图像的QPushButton。没有成功到现在。以下是我的code。

I am struggling to set an background image for an QPushButton. No Success till now. Following is my code.

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()));
}

我使用的样式表中的映像位于同一个项目文件夹。
不要任何人有任何解决方案?

The image I am using in the stylesheet is located in the same project folder. Do anybody has any solution?

推荐答案

您必须平坦属性设置为true:

You have to set the flat attribute to true:

button1-> setFlat(真);

您还必须设置autofillbackground -

You also have to set the autofillbackground -

button1-> setAutoFillBackground(真);

您可能想看看QToolButton不需要它是平的,以渲染图像。我使用他们的应用程序,我目前写,他们看起来很不错:

You may want to look at QToolButton which doesn't require it to be flat in order to render an image. I'm using them in an app I'm writing at the moment and they look very nice:

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);

(我的图像被存储作为资源)

(My image is stored as a resource)

这篇关于设置背景图片QPushButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆