gpt4 book ai didi

c++ - QPainter 保留以前的绘图

转载 作者:可可西里 更新时间:2023-11-01 18:36:46 26 4
gpt4 key购买 nike

这是我第一次使用 Qt,我必须制作一个与 Qt 等效的 MSPaint。然而,我在画线时遇到了麻烦。我目前可以通过单击屏幕上的某处并在其他地方释放来绘制一条线,但是当我绘制第二条线时,前一条线将被删除。如何在绘制另一个项目时保留之前绘制的项目?

void Canvas::paintEvent(QPaintEvent *pe){
QWidget::paintEvent(pe);
QPainter p(this);
p.drawPicture(0,0,pic);
}

void Canvas::mousePressEvent(QMouseEvent *mp){
start = mp->pos();
}

void Canvas::mouseReleaseEvent(QMouseEvent *mr){
end = mr->pos();
addline();
}

void Canvas::addline()Q_DECL_OVERRIDE{
QPainter p(&pic);
p.drawLine(start,end);
p.end();
this->update();
}

Canvas是QWidget的派生类,它有2个QPoint属性start和end。

类主体:

class Canvas : public QWidget{
Q_OBJECT
private:
QPoint start;
QPoint end;
QPicture pic;
public:
Canvas(){paint = false;setAttribute(Qt::WA_StaticContents);}
void addline();
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent( QMouseEvent * );
//void mouseMoveEvent( QMouseEvent * );
void mouseReleaseEvent( QMouseEvent * );

};

最佳答案

QPicture 记录QPainter 命令。您还可以从其文档中阅读以下内容:

Note that the list of painter commands is reset on each call to the QPainter::begin() function.

并且带有绘画设备的QPainter 构造函数确实调用了begin()。所以每次删除旧的记录命令。

使用它可能听起来很诱人,因为它确实有一些优点,例如,它与分辨率无关,但这并不是绘图应用程序在现实中的工作方式。切换到 QPixmap,您的绘图将保留。

此外,不要忘记初始化像素图,因为默认情况下它是空的,您将无法在其上绘制。

Canvas() : pic(width,height) {...}

此外,如果您想像艺术画笔一样介绍画笔的概念,而不是QBrush,您可能想看看这个approach to draw the line .

编辑:请注意,您应该能够通过不多次调用 begin() 来防止 QPicture 丢失其内容。如果您创建一个画家,专用于仅在类范围内绘制,并在构造函数中调用 begin,则记录的不同绘制操作应该保持不变。但是随着它们数量的增加,将 QPicture 绘制到您的小部件将花费越来越多的时间。你可以通过同时使用 QPictureQPixmap 来解决这个问题,并绘制到两者上,使用图片记录 Action 和像素图以避免不断重绘图片,即使你会做双倍的工作,它仍然会更有效率,同时你仍然保留使用图片以不同分辨率重新栅格化或保存绘图历史的可能性。但我怀疑 QPicture 是否会做得很好,因为您的绘图应用程序开始形成实际的绘图应用程序,例如,当您开始使用 pixmap brushe stencils 等时。

关于c++ - QPainter 保留以前的绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32934054/

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