扩展Qt标准图标 [英] Extend Qt standard icons

查看:434
本文介绍了扩展Qt标准图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何扩展QStyle类提供的标准图标并支持Windows和Linux?

How would one extend the standard icons provided by the QStyle class with support for Windows and Linux in mind?

namespace Ui {
  class TVLoader;
}

class TVLoader : public QMainWindow
{
  Q_OBJECT

public:
  explicit TVLoader(QWidget *parent = 0) :
  QMainWindow(parent),
  ui(new Ui::TVLoader)
{
  ui->setupUi(this);
  ui->actionAdd_TV_Show->setIcon(style()->standardIcon(?)); // this is where I would need some kind of "Add" icon which unfortunately doesn't exist
}


推荐答案

如果你想提供自己的图标,你想要子类化QStyle,重新实现 standardIconImplementation ()插槽,并从中返回一个新图标。下面是一个例子:

You man want to subclass QStyle if you want to provide your own icons, reimplement the standardIconImplementation() slot in your subclass and return a new icon from there. Below is an example:

class MyProxyStyle : public QProxyStyle
{
    Q_OBJECT

public:
    MyProxyStyle(QStyle *style = 0) : QProxyStyle(style) { }

public slots:
    QIcon standardIconImplementation(StandardPixmap standardIcon,
                                     const QStyleOption *option = 0,
                                     const QWidget *widget = 0) const
    {
        // check the standardIcon parameter for the icon type 
        if (standardIcon==QStyle::SP_DesktopIcon)
        {
            // return your new icon here
            standardIcon = QStyle::SP_DirIcon;
        }
        return QProxyStyle::standardIconImplementation(standardIcon, option, widget);
    }
};

这里是如何使用它:

// set new style for your widget
setStyle(new MyProxyStyle(style()));
// return different icon for QStyle::SP_DesktopIcon
action0->setIcon(style()->standardIcon(QStyle::SP_DesktopIcon));

希望这有助于您,

这篇关于扩展Qt标准图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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