如何使用样式表设置Qt按钮的图标? [英] How to set a Qt button's icons using stylesheet?

查看:3179
本文介绍了如何使用样式表设置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.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 / 

工作(图标未显示)。



如果我使用。您似乎应该尝试设置 border-image 属性,而不是图标



要修复它,请将 FormStyleSheetString 函数更改为:

  QString FormStyleSheetString(const QString& name)
{
const QString thisItemStyle(QToolButton:enabled {border-image:url(+ name +_normal.png);}
QToolButton:disabled {border-image:url(+ name +_pressed.png);
;}
);

return thisItemStyle;
}


I would like to set a button's icons using stylesheets, like this :

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

I compiled it like this :

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

Unfortunately, the above doesn't work (the icon is not shown).

If I use setIcon, then the icon is shown properly.

So, what am I doing wrong? How to set the button's icon using style sheet?

The images I used are :

解决方案

There was similar unanswered question before, right here. It seems that you should try setting border-image property instead of icon one.

To fix it, change FormStyleSheetString function to this :

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;
}

这篇关于如何使用样式表设置Qt按钮的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆