我如何使我的图像成为qt中的按钮?

问题描述 投票:0回答:2

我已经尝试在网上到处查找它,到目前为止还没有定论。我已经制作了三个png文件,其中两个用于悬停和按下状态。我曾尝试使用以下代码编辑常规按钮的样式表,但没有任何反应

QPushButton#myPushButton
{
    background-color: transparent;
    background-image: url(":C:\Users\imagepath");
    background-repeat: none; rgb(255, 255, 255)   
    border: none;
}
QPushButton#myPushButton:hover
{
   background-image: url(":C:\Users\imagepath");
}

QPushButton#myPushButton:pressed
{
   background-image: url(":C:\Users\imagepath");
}
qt stylesheet qt-creator
2个回答
1
投票

您可以创建一个Qt资源文件,将图像添加到资源中,然后选择它们并作为背景添加到QPushButton中。

这是一个很好的选择。

例如:

就我而言,我有一个Qt资源文件,名为“ myResource”,前缀为“ / ui”,以及一个名为“ myImg.png”的图像。

在我的QPushButton样式表上,我使用以下代码:

background-image: url(:/ui/myImg.png);

0
投票

好吧,您可以在按钮上放置图像,这里是详细说明:How to set image on QPushButton?^(指向堆栈溢出网站的链接)

© www.soinside.com 2019 - 2024. All rights reserved.