Question

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
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top