c++ - 扩展 Qt 标准图标

标签 c++ qt qt4 cross-platform icons

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

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 () 在您的子类中插入并从那里返回一个新图标。下面是一个例子:

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

希望这对你有帮助,问候

关于c++ - 扩展 Qt 标准图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050869/

相关文章:

c++ - QToolButton 在 QFrame/QWidget 中的视觉行为

c++ - 在调整 QWidget 大小时调整 QWidget 中的 QTextEdit 大小

c++ - QML/real 和 C++/float 之间的 Qt 类型错误

c++ - 纹理在 Qt 中缩放而不是重复

c++ - 与第 3 方库链接导致 openssl 中出现段错误

qt - 使用 QtAV 制作视频缩略图

c++ - 如何使用带有模板的本地类?

c++ - 逐步将 gcov 与 CMake 结合使用

c++ - 使用 std::async 调用采用 unique_ptr 的函数

c++ - 将导出的符号保留在共享库中