Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
6.9.0
-
None
Description
I'm trying to draw a png image (using QPixmap) in a QLabel with Qt6. I resize the pixmap to fit the QLabel size. It looks just fine when DPI is set to 100%. But on my laptop, where DPI is set to 125% (to make text bigger, this is the default recommended config on the laptop), the pixmap looks ugly (the lines are blured). See screenshot:
The code is very simple:
main.cpp:
#include <QApplication> #include "mainwindow.h" int main( int argc, char* argv[] ) { QApplication app(argc, argv); MainWindow wnd; wnd.show(); return app.exec(); }
mainwindow.h:
#pragma once #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget* parent = NULL); };
mainwindow.cpp:
#include "mainwindow.h" #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> #include <QApplication> #include <QLabel> MainWindow::MainWindow( QWidget* parent ) : QMainWindow(parent) { QWidget* centralWidget = new QWidget(this); centralWidget->setLayout(new QVBoxLayout(centralWidget)); QPushButton* button = new QPushButton(centralWidget); button->setIcon(QIcon(":/qtbug_pixmapscale/image.png")); QWidget* labelsWidget = new QWidget(centralWidget); labelsWidget->setLayout(new QHBoxLayout(labelsWidget)); QLabel* titleLabel = new QLabel(centralWidget); titleLabel->setText("This is ugly:"); QLabel* iconLabel = new QLabel(centralWidget); int maxHeight = titleLabel->sizeHint().height() + 10; iconLabel->setMaximumSize( QSize(maxHeight,maxHeight) ); iconLabel->setPixmap(QPixmap(":/qtbug_pixmapscale/image.png").scaled(iconLabel->maximumSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); labelsWidget->layout()->addWidget(titleLabel); labelsWidget->layout()->addWidget(iconLabel); centralWidget->layout()->addWidget(button); centralWidget->layout()->addWidget(labelsWidget); setCentralWidget(centralWidget); }
Note:
using `setDevicePixelRatio` is a good workaround:
auto pixmap = QPixmap(":/qtbug_pixmapscale/image.png").scaled(iconLabel->maximumSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmap.setDevicePixelRatio(qApp->primaryScreen()->devicePixelRatio());
iconLabel->setPixmap(pixmap);
But it would be much better if it could be done automatically by QtPixmap class itself. One should not have to do this for every QPixmap it wants to show. Note that QIcon apparently handles this correctly.