gpt4 book ai didi

c++ - 为什么 QPixmap::scaled() 不起作用?

转载 作者:太空狗 更新时间:2023-10-29 19:58:49 25 4
gpt4 key购买 nike

我正在使用 QPixmap 和 QPainter 编写简单的应用程序。在我的程序中,我需要加载一些图像并调整它们的大小。我使用了 QPixmap::scaled(),但图像没有缩放。我做错了什么?这是我的代码:

棋子.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);
}

切斯蒂尔.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

最佳答案

您会从文档中注意到 QPixmap::scaled 成员函数是 const - 即它不会更改对象本身。

缩放后的对象由该方法返回,它不会改变原始像素图。

尝试这样的事情:

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

QPainter painter(this);
painter.drawPixmap(0, 0, scaled)

关于c++ - 为什么 QPixmap::scaled() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17504653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com