How to set Qt button icons using stylesheet?

I would like to set button icons using style sheets, for example:

#include <QToolButton>
#include <QApplication>

QString FormStyleSheetString( const QString & name )
{
  const QString thisItemStyle( "QToolButton:enabled { image: url(" + name + "_normal.png); }  "
                             "QToolButton:pressed { image: url(" + name + "_pressed.png); }  "
                             "QToolButton:disabled { image: url(" + name + "_disabled.png); }  "
                           );

  return thisItemStyle;
}

int main(int argc, char * argv[])
{
    QApplication qapp(argc,argv);

    QToolButton button;
    button.setStyleSheet( FormStyleSheetString( "button" ) );
    button.setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    button.setIconSize(QSize(200,200));
    button.show();

    return qapp.exec();
}

I compiled it as follows:

g++ -O3 -std=c++0x -Wall -Wextra -pedantic test.cpp -lQtCore -lQtGui -I/usr/include/Qt/ -I/usr/include/QtCore/ -I/usr/include/QtGui/

Unfortunately, the above does not work (the icon does not appear).

If I use setIcon , the icon displays correctly.

So what am I doing wrong? How to set a button icon using a stylesheet?

Images I used:
button_normal.pngbutton_pressed.pngbutton_disabled.png

0
source share
1 answer

Previously there was a similar unanswered question, right here . It seems that you should try setting the property border-imageinstead of iconone.

, FormStyleSheetString :

QString FormStyleSheetString( const QString & name )
{
  const QString thisItemStyle( "QToolButton:enabled { border-image: url(" + name + "_normal.png); }  "
                               "QToolButton:pressed { border-image: url(" + name + "_pressed.png); }  "
                               "QToolButton:disabled { border-image: url(" + name + "_disabled.png); }  "
                           );

  return thisItemStyle;
}
+3

All Articles