image - 设置 QPushButton 的背景图片

标签 image qt4 background

我正在努力为 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()));
}

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

最佳答案

您必须将 flat 属性设置为 true:
button1->setFlat(true);
您还必须设置自动填充背景 -
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);

(我的图像存储为资源)

关于image - 设置 QPushButton 的背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2671842/

相关文章:

css - 使用 IF 语句将类添加到 Rails 中的 image_tag

iphone - RootViewController - TableView :cellForRowAtIndexPath - Load image from assetsLibrary

linux - 安装qt4时出现问题: no rule to make target "install-strip"

CSS 背景属性 - 速记与长格式

jquery - 等到加载背景(css)中的图像

iOS 背景图像在设备上不起作用

html - 2 行响应图像

image - OpenCV中的Concat图像

qt4 - 横向模式损坏,显示不正确

qt - 如何在 QTabWidget 中的两个选项卡之间插入空格?