8

Is it possible to set and change the icon on a QPushButton using stylesheets?

I need this for a Windows-based white-label Qt4.5 application that customers stylize using stylesheets.

Robin
  • 695
  • 2
  • 8
  • 23

3 Answers3

8

You can add the icon by changing the QAbstractButton-property. Simply use:

QAbstractButton {
    qproperty-icon: theme_url("/button_action/add");
}

The type-selector QAbstractButton, enables you to set the style for the selected object and all its subclasses.

Since QPushButton extends QAbstractButton, this will do the trick. (Of course you could also use QPushButton type-selector here).

For more information on how to use selectors, I found this quite helpful.

If you want to learn more about the button-properties, refer to the Qt-Documentation here.

PS. In case you want to change the icon, once the button is clicked, use:

qproperty-icon: url(":/Icons/before_click.png") off,
                url(":/Icons/after_click.png") on ;
Kim
  • 1,361
  • 3
  • 18
  • 24
0

Yup.

border-image: url(/url/to/image);
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • 1
    Thanks for replying. This appears to set the background of my QPushButton to the image rather than the QPushButton's icon. Any ideas what I've done wrong? Thanks. – Robin Apr 20 '10 at 15:40
  • Huh, I thought that set the icon, with `background-image:` setting the background. I'd have to play with it, apparently my memory is not what it used to be. – Brian Roach Apr 20 '10 at 17:05
  • It looks like you have to style the icon size, the alignment, and the text alignment so everything lays out correctly. I don't have time to mess with it right now, but everything I read via a couple google searches says that `border-image:` is the icon in regard to the stylesheet. – Brian Roach Apr 20 '10 at 17:13
  • You can use `border-image` to change the appearance of the button, e. g. with a dotted border or ornaments. It has nothing to do with the icon. – Andreas Haferburg Jun 25 '20 at 10:15
-2

You can use:

image: url(/url/to/image);

See http://doc.qt.io/qt-5/stylesheet-reference.html#image-prop and http://doc.qt.io/qt-5/stylesheet-customizing.html#box-model.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Jean Regisser
  • 6,636
  • 4
  • 32
  • 32