0

I have tried looking it up everywhere on the net, nothing conclusive so far. i have made three png files two of which are for the hover and pressed states. i have tried editing the style sheet of a regular push button with the following code but nothing happens

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");
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Why would you do this with stylesheet? – László Papp May 23 '14 at 04:51
  • Searching for "QPushButton Image" reveals first link http://stackoverflow.com/questions/3137805/how-to-set-image-on-qpushbutton for using the QPushButton::setIcon() – Sebastian Lange May 23 '14 at 05:22
  • Even better, searching for "QPushButton Image Stylesheet" reveals first link as: http://stackoverflow.com/questions/2675925/how-to-change-qpushbutton-icon-using-stylesheets-in-qt-app – Sebastian Lange May 23 '14 at 05:23
  • 1
    And see http://stackoverflow.com/questions/17874898/cannot-load-image-for-qlabel-background for formatting a resource identifier – Jens May 23 '14 at 07:14
  • You could give the buttons images : https://stackoverflow.com/questions/3137805/how-to-set-image-on-qpushbutton – Omer Dec 16 '19 at 16:56

1 Answers1

1

You could create a Qt resource file, to add your images to resource and then to choose them and add as background in your QPushButton.

That is a good alternative.

For example:

In my case I have a Qt resource file called "myResource" with a prefix "/ui" and an image called "myImg.png".

On my QPushButton stylesheet i used the following code:

background-image: url(:/ui/myImg.png);
Adriano Leal
  • 316
  • 1
  • 5
  • 20