SVG QIcon 无法返回要在 QToolButton 中使用的放大像素图



我在图标中使用SVG图像QToolButton。但是我需要能够更改工具按钮的大小,并且我需要图标来放大或缩小。我的 SVG 图标的原生大小为 24(像素(。QIcon的问题在于它们不会扩展到本机大小以上。(请参阅尝试0...在我的代码中(

所以我尝试了另一种方法,我覆盖了resizeEvent并使用了QToolButton.setIconSize(event.size()),它完美地工作,它允许无限制地扩展和缩减。(请参阅尝试 1...在我的代码中(

但是我的问题来了。我确实需要对渲染的图标进行一些像素级的颜色转换。因此,我需要获得可以从QPixmap获得QImage但问题是我无法将 SVG 图像渲染到大于 SVG 本机大小的像素图。像素图本身更大,但图像仍然很小,与像素图的中心对齐。我尝试了三种不同的方法,但没有一种奏效。(请参阅尝试 2...尝试4...在我的代码中(

我的代码在这里。为了简单起见,我省略了我对获得的像素图/图像进行的颜色像素化转换。

工具栏.h

#pragma once
#include <QToolButton>
class ToolButton : public QToolButton
{
Q_OBJECT
public:
explicit ToolButton(QWidget *parent = nullptr);
protected:
void resizeEvent(QResizeEvent *event) override;
private:
void attempt0_thisDoesNotScaleUp();
void attempt1_thisScalesWellButIdoNotHavePixmap(int w, int h);
void attempt2_thisDoesNotScaleUp(int w, int h);
void attempt3_thisDoesNotScaleUp(int w, int h);
void attempt4_thisDoesNotScaleUp(int w, int h);
QString m_iconName;
};

工具栏.cpp

#include "toolbutton.h"
#include <QDebug>
#include <QPainter>
#include <QResizeEvent>
#include <QtSvg/QSvgRenderer>
ToolButton::ToolButton(QWidget *parent) :
QToolButton(parent)
{
m_iconName = "icon.svg";
// attempt0_thisDoesNotScaleUp(); // uncomment to try attempt 0
}
void ToolButton::resizeEvent(QResizeEvent *event)
{
int w = event->size().width();
int h = event->size().height();
//attempt1_thisScalesWellButIdoNotHavePixmap(name, w, h); // uncomment to try attempt 1
//attempt2_thisDoesNotScaleUp(name, w, h); // uncomment to try attempt 2
//attempt3_thisDoesNotScaleUp(name, w, h); // uncomment to try attempt 3
//attempt4_thisDoesNotScaleUp(name, w, h); // uncomment to try attempt 4
QToolButton::resizeEvent(event);
}
void ToolButton::attempt0_thisDoesNotScaleUp()
{
setIcon(QIcon(m_iconName));
}
void ToolButton::attempt1_thisScalesWellButIdoNotHavePixmap(int w, int h)
{
setIcon(QIcon(m_iconName));
setIconSize(QSize(w, h));
}
void ToolButton::attempt2_thisDoesNotScaleUp(int w, int h)
{
QIcon source(m_iconName);
QPixmap pixmap = source.pixmap(w, h);
QIcon icon;
icon.addPixmap(pixmap);
setIcon(icon);
}
void ToolButton::attempt3_thisDoesNotScaleUp(int w, int h)
{
QSvgRenderer renderer(m_iconName);
QPixmap pm(w, h);
QPainter painter(&pm);
renderer.render(&painter, pm.rect());
QIcon icon;
icon.addPixmap(pm);
setIcon(icon);
}
void ToolButton::attempt4_thisDoesNotScaleUp(int w, int h)
{
QIcon source(m_iconName);
QPixmap pm(w, h);
QPainter painter(&pm);
source.paint(&painter, pm.rect());
QIcon icon;
icon.addPixmap(pm);
setIcon(icon);
}

main.cpp(将工具按钮显示为主窗口,以便能够测试大小调整(

#include "toolbutton.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ToolButton w;
w.show();
return a.exec();
}

为了进行测试,我使用的是原始大小为 24x24 像素的 www.iconmonstr.com SVG 图标。当然,我需要能够将它们扩展到这个大小以上。

我想我错过了一些明显的东西...因为QToolButton::setIconSize(someBiggerSize);能够放大 SVG 图标。

可能是我在SO上公开的最愚蠢的错误:如果我把setIconSize(QSize(w, h));放在每个attempt...等方法的末尾,它们都会正常工作。

尽管这个错误很愚蠢,但我还是会在这里留下答案,作为我们可以使用多少种方法来渲染 SVG 的参考。

更新:还有另一种方法可以将SVG直接渲染到这里提到的QImage如何将缩放的SVG渲染到QImage?这个实际上似乎最适合我的目的。无需中间像素图。

最新更新