Skip to content

Instantly share code, notes, and snippets.

@SC-One
Last active October 23, 2022 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SC-One/c56caade8fa44562b75511f41192641e to your computer and use it in GitHub Desktop.
Save SC-One/c56caade8fa44562b75511f41192641e to your computer and use it in GitHub Desktop.
//IconLabel is subclass of QLabel
//Note: if you wanna keep Quality , you should create pixmap for all sizes (so override resizeEvent !)
// this method is working normally
QPixmap IconLabel::FromSvgToPixmap(const QString &SvgFile,
const QSize &ImageSize) {
QSvgRenderer SvgRenderer(SvgFile);
QPixmap Image(ImageSize);
QPainter Painter;
Image.fill(Qt::transparent);
Painter.begin(&Image);
SvgRenderer.render(&Painter);
Painter.end();
return Image;
}
// this method is working normally too but when we have a monitor that has more pixle ratio we should
// improve shrunkened image that keep real size
QPixmap IconLabel::FromSvgToPixmap(const QSize &ImageSize,
const QString &SvgFile) {
const qreal PixelRatio = qApp->primaryScreen()->devicePixelRatio();
QSvgRenderer SvgRenderer(SvgFile);
QPixmap Image(ImageSize * PixelRatio);
QPainter Painter;
Image.fill(Qt::transparent);
Painter.begin(&Image);
SvgRenderer.render(&Painter);
Painter.end();
Image.setDevicePixelRatio(PixelRatio);
return Image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment