Skip to content

Commit e3f691e

Browse files
committed
PenLayer: Fix a crash when destroying the painter
1 parent b126800 commit e3f691e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/penlayer.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ PenLayer::~PenLayer()
1919
{
2020
if (m_engine)
2121
m_projectPenLayers.erase(m_engine);
22-
23-
if (m_painter && m_painter->isActive())
24-
m_painter->end();
2522
}
2623

2724
bool PenLayer::antialiasingEnabled() const
@@ -55,11 +52,7 @@ void PenLayer::setEngine(libscratchcpp::IEngine *newEngine)
5552
m_fbo = std::make_unique<QOpenGLFramebufferObject>(m_engine->stageWidth(), m_engine->stageHeight(), m_fboFormat);
5653
Q_ASSERT(m_fbo->isValid());
5754

58-
if (m_painter && m_painter->isActive())
59-
m_painter->end();
60-
6155
m_paintDevice = std::make_unique<QOpenGLPaintDevice>(m_fbo->size());
62-
m_painter = std::make_unique<QPainter>(m_paintDevice.get());
6356
clear();
6457
}
6558

@@ -86,14 +79,15 @@ void scratchcpprender::PenLayer::drawPoint(const PenAttributes &penAttributes, d
8679

8780
void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, double x0, double y0, double x1, double y1)
8881
{
89-
if (!m_fbo || !m_painter || !m_engine)
82+
if (!m_fbo || !m_paintDevice || !m_engine)
9083
return;
9184

9285
// Begin painting
9386
m_fbo->bind();
94-
m_painter->beginNativePainting();
95-
m_painter->setRenderHint(QPainter::Antialiasing, m_antialiasingEnabled);
96-
m_painter->setRenderHint(QPainter::SmoothPixmapTransform, false);
87+
QPainter painter(m_paintDevice.get());
88+
painter.beginNativePainting();
89+
painter.setRenderHint(QPainter::Antialiasing, m_antialiasingEnabled);
90+
painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
9791

9892
// Translate to Scratch coordinate system
9993
double stageWidthHalf = m_engine->stageWidth() / 2;
@@ -107,16 +101,17 @@ void scratchcpprender::PenLayer::drawLine(const PenAttributes &penAttributes, do
107101
QPen pen(penAttributes.color);
108102
pen.setWidthF(penAttributes.diameter);
109103
pen.setCapStyle(Qt::RoundCap);
110-
m_painter->setPen(pen);
104+
painter.setPen(pen);
111105

112106
// If the start and end coordinates are the same, draw a point, otherwise draw a line
113107
if (x0 == x1 && y0 == y1)
114-
m_painter->drawPoint(x0, y0);
108+
painter.drawPoint(x0, y0);
115109
else
116-
m_painter->drawLine(x0, y0, x1, y1);
110+
painter.drawLine(x0, y0, x1, y1);
117111

118112
// End painting
119-
m_painter->endNativePainting();
113+
painter.endNativePainting();
114+
painter.end();
120115
m_fbo->release();
121116

122117
update();

src/penlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <ipenlayer.h>
66
#include <QOpenGLFramebufferObject>
77
#include <QOpenGLPaintDevice>
8+
#include <QPainter>
89
#include <scratchcpp/iengine.h>
910

1011
namespace scratchcpprender
@@ -46,7 +47,6 @@ class PenLayer : public IPenLayer
4647
libscratchcpp::IEngine *m_engine = nullptr;
4748
std::unique_ptr<QOpenGLFramebufferObject> m_fbo;
4849
std::unique_ptr<QOpenGLPaintDevice> m_paintDevice;
49-
std::unique_ptr<QPainter> m_painter;
5050
QOpenGLFramebufferObjectFormat m_fboFormat;
5151
};
5252

0 commit comments

Comments
 (0)