2011-12-05 27 views
0

私はこのように、スタイルシートを使用して、ボタンのアイコンを設定したいと思います:スタイルシートを使用してQtボタンのアイコンを設定する方法は?

#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(); 
} 

私はこのようにそれをコンパイル:

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

残念ながら、上記(アイコンが表示されていない動作しません。 )。

setIconを使用すると、アイコンが正しく表示されます。

私は間違っていますか?スタイルシートを使用してボタンのアイコンを設定する方法は?私が使用し

イメージは以下のとおりです。
button_normal.png button_pressed.png button_disabled.png

答えて

3

前に同様の未回答の質問、右hereがありました。 iconの代わりにborder-imageプロパティを設定してみてください。それを修正する

、これに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; 
} 
+0

感謝を。それは今働く。 –

関連する問題