2011-12-05 15 views
5

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

#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.setText("some thing..."); 
    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

PS私は同様の質問hereを尋ねたが、テキストは(アイコンがすべて踏み付けさに設定されれば答えは動作しません注意してくださいテキストはアイコンの下にありません)。

EDIT 1:

QString FormStyleSheetString(const QString & name) 
{ 
    const QString thisItemStyle("QToolButton { qproperty-icon: url(" + name + "_normal.png); }; " 
           "QToolButton:pressed { qproperty-icon: url(" + name + "_pressed.png); }; " 
           "QToolButton:hover { qproperty-icon: url(" + name + "_disabled.png); }; " 
           ); 

    return thisItemStyle; 
} 

それもうまくいきませんでした: (カミルKlimekが示唆したように)私もこの機能を試してみました。ボタンを押すか、ホバリングしてもアイコンは変わりません。

+0

アイコンではなく画像を設定します。 try qproperty-icon:url() –

+0

@ KamilKlimek私はそれを試みたが、うまくいかなかった。これは通常の画像を設定しますが、押されずにホバーしません。私はそれがこのバグのためだと思う:https://bugreports.qt.nokia.com/browse/QTBUG-2982?page=com.atlassian.streams.streams-jira-plugin:activity-stream-issue-tab –

+0

あなたはイメージまたは背景としてそれを設定する必要があります!しかし、! CSSでサイズを調整する必要があります。 –

答えて

8

その日、私は何とか問題を解決するために管理しますが、解決策を投稿するのを忘れ:

QString FormStyleSheetString(const QString & name) 
{ 
    const QString thisItemStyle(
    "QToolButton {\n" 
       " border: none;\n" 
       " background: url(" + name + "_normal.png) top center no-repeat;\n" 
       " padding-top: 200px;\n" 
       " width: 200px;\n" 
       " font: bold 14px;\n" 
       " color: red;\n" 
       "}\n" 
       "QToolButton:hover {\n" 
       " background: url("+name+"_hover.png) top center no-repeat;\n" 
       " color: blue;\n" 
       "}\n" 
       "QToolButton:pressed {\n" 
       " background: url("+name+"_pressed.png) top center no-repeat;\n" 
       " color: gray;\n}"); 

    return thisItemStyle; 
} 

ちょうど背景を設定するのに十分ではなかったです。また、固定サイズが必要でした。

関連する問題