jpo38
jpo38

Reputation: 21514

Why QPixmap looks ugly on laptop when DPI is set to 125%

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

enter image description here

In comparison, I set the same png image as icon to a QPushButton and it looks just fine.

Is there anything I'm doing wrong? How can the code be fixed to make the image look as nice as in the QPushButton?

Here is the full code:

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

main.cpp:

#include <QApplication>
#include "mainwindow.h"

// Qtbug where QPixmap drawn in a QLabel looks ugly on laptop when display is set to 125%

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

};

pixmapscale.qrc:

image.png

image.png: enter image description here

Upvotes: 0

Views: 116

Answers (2)

jpo38
jpo38

Reputation: 21514

As QIcon set to QPushButton looks good, I just found a workaround:

iconLabel->setPixmap(QIcon(":/qtbug_pixmapscale/image.png").pixmap(iconLabel->size()));

It looks better, as QIcon class apparently handles DPI factor smartly.

enter image description here

Upvotes: 1

jpo38
jpo38

Reputation: 21514

As proposed by musicamante, using QPixmap::setDevicePixelRatio solves the problem:

auto pixmap = QPixmap(":/qtbug_pixmapscale/image.png").scaled(iconLabel->maximumSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
pixmap.setDevicePixelRatio(qApp->primaryScreen()->devicePixelRatio());
iconLabel->setPixmap(pixmap);

Upvotes: 1

Related Questions