【问题标题】:How to set a Qt button's icons using stylesheet?如何使用样式表设置 Qt 按钮的图标?
【发布时间】:2011-12-05 07:58:56
【问题描述】:

我想使用样式表设置按钮的图标,如下所示:

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

我是这样编译的:

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,则图标显示正确。

那么,我做错了什么?如何使用样式表设置按钮的图标?

我使用的图片是:

【问题讨论】:

    标签: c++ qt4 stylesheet


    【解决方案1】:

    之前有类似的未回答的问题,对here。看来您应该尝试设置border-image 属性而不是icon 一个。

    要修复它,请将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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 2012-01-13
      • 2014-07-17
      • 2014-03-14
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多