Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why QPixmap::scaled() does not work?

Tags:

c++

qt

qpixmap

I'm writing simple application by using QPixmap and QPainter. In my programm I need to load some images and resize them. I used QPixmap::scaled(), but images does not scaled. What I did wrong? This is my code:

chesstile.cpp

#include "chesstile.h"

ChessTile::ChessTile(QWidget *parent) :
    QLabel(parent)
{
}

void ChessTile::paintEvent(QPaintEvent *)
{
    const QString &fileName = "images/white_king.png";
    QPixmap bgPixmap(fileName);
    bgPixmap.scaled(QSize(64, 64));
    QPainter painter(this);
    painter.drawPixmap(0, 0, bgPixmap);
}

chesstile.h

#ifndef CHESSTILE_H
#define CHESSTILE_H

#include <QLabel>
#include <QString>
#include <QPainter>
#include <QPixmap>
#include <QSize>

class ChessTile : public QLabel
{
    Q_OBJECT
public:
    ChessTile(QString fileName,
              QString tileColor,
              QWidget *parent = 0);
    void paintEvent(QPaintEvent *);

private:

signals:

public slots:

};

#endif // CHESSTILE_H
like image 686
Ilya Glushchenko Avatar asked Jul 09 '26 17:07

Ilya Glushchenko


1 Answers

You'll notice from the docs that the QPixmap::scaled member function is const - i.e. it doesn't change the object itself.

The scaled object is returned by that method, it doesn't change the original pixmap.

Try something like:

QPixmap bgPixmap(fileName);
QPixmap scaled = bgPixmap.scaled(QSize(64, 64));

QPainter painter(this);
painter.drawPixmap(0, 0, scaled)
like image 90
Mat Avatar answered Jul 12 '26 11:07

Mat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!