Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

png image is blurred on QLabel



  • If the QLabel size is smaller than the size of PNG image, the display of image(mainly texts) is blurred.
    No problem when the label size is same or bigger as the image size.

    QFile file(filename);
    QPixmap *pixmap = new QPixmap();
    if(!pixmap->load(filename))
    {
        delete pixmap;
        pixmap = Q_NULLPTR;
    }
    

    ui.label->setPixmap( pixmap->scaled( size(), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );

    googled online and did not find the right solutions. Thanks for your help in advance.



  • class MyImage : public QWidget
    {
    Q_OBJECT
    public:
    explicit MyImage(QWidget* parent = Q_NULLPTR)
    : QWidget(parent)
    {}
    const QPixmap& pixmap() const
    {
    return m_pixmap;
    }
    void setPixmap(const QPixmap& px)
    {
    m_pixmap = px;
    update();
    }
    protected:
    void paintEvent(QPaintEvent*)
    {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setRenderHint(QPainter::TextAntialiasing, true);
    style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, m_pixmap.scaled(rect().size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    private:
    QPixmap m_pixmap;
    };

    also tried to use paintEvent of QWidget to display it. Got the same result.



  • It'll be useful if you can post screenshot of non-resized (and hence not blurred) and resized/blurred version of the image.
    Also, note that scaling down images will almost always suffer from quality loss.


Log in to reply