C++ 如何设置Qt按钮';使用样式表的图标?
我想使用样式表设置按钮图标,如下所示:C++ 如何设置Qt按钮';使用样式表的图标?,c++,qt4,stylesheet,C++,Qt4,Stylesheet,我想使用样式表设置按钮图标,如下所示: #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.show();
return qapp.exec();
}
不幸的是,上述操作不起作用(图标未显示)
如果使用,则图标会正确显示
那么,我做错了什么?如何使用样式表设置按钮图标
我使用的图像是:以前也有类似的未回答问题,对吧。似乎您应该尝试设置
边框图像
属性,而不是图标
属性
要修复此问题,请将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;
}
以前也有类似的未回答的问题,对吧。似乎您应该尝试设置
边框图像
属性,而不是图标
属性
要修复此问题,请将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;
}