gpt4 book ai didi

C++/Qt : change (the size of) a QImage that is being painted

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:07 24 4
gpt4 key购买 nike

如果我提供了不必要的详细信息,我深表歉意。我有一个看起来像这样的 Canvas 类:

class Canvas : public QWidget
{
Q_OBJECT
public:
explicit Canvas(int width = 700, int height = 700, QWidget *parent = 0);
void setDelegate(CanvasDelegate *delegate);
private:
CanvasDelegate *delegate;
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *resizeEvent);
[...]
};

Canvas::paintEvent(QPaintEvent *)函数是这样实现的:

void Canvas::paintEvent(QPaintEvent *)
{
delegate->redrawBuffer();
QPainter canvas_painter(this);
canvas_painter.drawImage(0, 0, *(delegate->getImage()));
}

所以 CanvasDelegate 类看起来像这样:

class CanvasDelegate
{
friend class Canvas;

public:
CanvasDelegate(const Canvas *canvas);
~CanvasDelegate();

const QImage * getImage() const;

void drawPoint(const Complex &z, const QColor &color = "black", int width = 3);
[...]
virtual void redrawBuffer(const H2Isometry &mobius = H2Isometry::identity()) = 0;
virtual void mousePress(QMouseEvent * mouseEvent) = 0;
[...]


protected:
const Canvas *canvas;

int sizeX, sizeY;
[...]

QPen *pen;
QImage *image;
QPainter *painter;

void rescale(int sizeX, int sizeY);
};

CanvasDelegate的构造函数如下:

CanvasDelegate::CanvasDelegate(const Canvas *canvas) : canvas(canvas)
{
pen = new QPen;
image = new QImage(canvas->width(), canvas->height(), QImage::Format_RGB32);
painter = new QPainter(image);
[...]
}

我不确定这是有史以来最好的设计,但这不是我的问题(尽管欢迎任何评论)。我的问题是调整窗口 (Canvas) 大小时会发生什么。这是我的代码的样子:

void Canvas::resizeEvent(QResizeEvent *resizeEvent)
{
QSize newSize = resizeEvent->size();
delegate->rescale(newSize.width(), newSize.height());
//update();
}


void CanvasDelegate::rescale(int sizeX, int sizeY)
{
*image = QImage(sizeX, sizeY, QImage::Format_RGB32);
painter->eraseRect(0, 0, sizeX, sizeY);

this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}

问题是当我运行程序时,它崩溃了。显然,在 void CanvasDelegate::rescale(int sizeX, int sizeY) 中调用 painter->eraseRect(0, 0, sizeX, sizeY); 时存在段错误.我不明白为什么,我不明白问题是什么。

在以前的版本中,我写了以下内容(现在看来比必要的更复杂):

void CanvasDelegate::rescale(int sizeX, int sizeY)
{
QImage * oldImage = image;
QImage * newImage = new QImage(sizeX, sizeY, QImage::Format_RGB32);

QPainter * oldPainter = painter;
QPainter * newPainter = new QPainter(newImage);
newPainter->eraseRect(0, 0, sizeX, sizeY);
newPainter->setPen(*pen);

image = newImage;
painter = newPainter;

delete oldImage;
delete oldPainter;

this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}

但这不起作用:我收到 Qt 错误 QPaintDevice:无法破坏正在绘制的绘制设备。如果我删除 delete oldImage;delete oldPainter;,一切正常,但这是一个令人作呕的内存泄漏,不是吗。

有人明白为什么我写的东西不起作用,我需要做什么吗?

非常感谢您的关注。

最佳答案

我不确定为什么 painter->eraseRect(0, 0, sizeX, sizeY); 段错误,但可能是当 QPainter 的 paintdevice 是图像时,它的大小不应该改变,因此 *image = QImage(sizeX, sizeY, QImage::Format_RGB32); 打破了这个假设。

因此,我会尝试在调整图像大小之前删除 QPainter,然后调整图像大小,然后分配一个新的 QPainter。在代码中:

void CanvasDelegate::rescale(int sizeX, int sizeY)
{
delete painter;
*image = QImage(sizeX, sizeY, QImage::Format_RGB32);
painter = new QPainter(image);
painter->eraseRect(0, 0, sizeX, sizeY);
painter->setPen(*pen);

this->sizeX = sizeX;
this->sizeY = sizeY;
[...]
}

关于C++/Qt : change (the size of) a QImage that is being painted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197544/

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