开发者

Extend Qt standard icons

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开发者_Python百科 some kind of "Add" icon which unfortunately doesn't exist
}


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

here's how can you use it:

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

hope this helps, regards


Since 4.6, Qt can use freedesktop icon theme:

QIcon undo_icon = QIcon::fromTheme("edit-undo");

But there is no icon themes in Windows (and MacOS). However, you can use any theme you want there, all you need is put this theme into (or part of it) into :/icons resource directory and do following in main():

if (!QIcon::hasThemeIcon("document-open")) {
    QIcon::setThemeName("/");
}

(it is hack for QTBUG-16697).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜