C++ 如何设置QToolButton';使用样式表的图标?
我想使用样式表设置QToolButton的图标,如下所示:C++ 如何设置QToolButton';使用样式表的图标?,c++,qt4,stylesheet,C++,Qt4,Stylesheet,我想使用样式表设置QToolButton的图标,如下所示: #include <QToolButton> #include <QApplication> QString FormStyleSheetString( const QString & name ) { const QString thisItemStyle( "QToolButton:enabled { image: url(" + name + "_normal.png); } "
#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();
}
不幸的是,上述操作不起作用(图标未显示)
如果使用,则图标会正确显示
那么,我做错了什么?如何使用样式表设置按钮图标
我使用的图像是:请注意,我问了类似的问题,但一旦文本设置好,答案就不起作用了(图标被压扁了,文本不在图标下方) 编辑1: 我还尝试了这个函数(正如Kamil Klimek所建议的):
但它也不起作用。按下按钮或悬停不会更改图标。那天晚些时候,我设法解决了这个问题,但忘记发布解决方案:
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;
}
仅仅设置背景是不够的。它还需要固定大小。您设置的是图像而不是图标。试试qproperty图标:url()@KamilKlimek我试过了,但没用。它将设置正常图像,但不会按下并悬停。我认为这是因为这个错误:然后你必须将它设置为图像或背景!但是您必须使用CSS调整大小。@KamilKlimek如果我将其设置为背景,则文本居中。将其设置为图像不起作用(图像未显示)。能否尝试使用“背景图像”而不是“图像”。
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;
}
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;
}