我想使用样式表来设置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,那么图标会正确显示。
那么,我做错了什么?如何使用样式表设置按钮的图标?
我使用的图片是:
备注:请注意,我在这里提出过类似的问题,但是答案在设置文本后失效(图标被压缩,文本不在图标下方)。
编辑1: 我也尝试了Kamil Klimek建议的这个函数:
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;
}
但它也没有起作用。按下按钮或悬停都无法改变图标。