[英]How to repaint in qt without removing the previous paint in the widget?
每次我在QWidget上重新繪制時,都會重新繪制並刪除以前的繪制。 進行重繪時如何保存以前的狀態?
您必須明確地做到這一點。 一種方法是在QImage
上進行繪制,然后在paintEvent
中將內容簡單化為小部件。
一種更簡單的方法是將所有圖形基元存儲在某種容器中,並根據需要繪制所有圖形基元。 Qt提供了這樣一個容器: QPicture
是一個圖形圖元文件,用於存儲圖形操作以供以后重播。
例如:
class PicLabel : public QLabel {
QPicture m_picture;
public:
PicLabel(const QString & text, QWidget * parent = 0) :
QLabel(text, parent) {}
PicLabel(QWidget * parent = 0) : QLabel(parent) {}
void addPoint(const QPointF & point) {
QPainter p(&m_picture);
p.drawPoint(point);
update();
}
void addLine(const QPointF & start, const QPointF & end) {
QPainter p(&m_picture);
p.drawLine(start, end);
update();
}
void clear() {
std::swap(m_picture, QPicture());
update();
}
protected:
void paintEvent(QPaintEvent * event) Q_DECL_OVERRIDE {
QLabel::paintEvent(event);
QPainter p(this);
p.drawPicture(0, 0, m_picture);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.