-
Notifications
You must be signed in to change notification settings - Fork 0
/
painteritem.cpp
72 lines (60 loc) · 1.39 KB
/
painteritem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "painteritem.h"
PainterItem::PainterItem(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
setAcceptedMouseButtons(Qt::LeftButton);
}
void PainterItem::paint(QPainter *painter)
{
painter->fillRect(0,0,100,100, Qt::black);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QPen(Qt::white, 5));
if(m_linePoints.size() > 1)
{
for(int i=1; i<(int) m_linePoints.size(); i++)
{
painter->drawLine(m_linePoints[i-1], m_linePoints[i]);
}
}
}
QImage PainterItem::toQImage()
{
qDebug() <<"toQImage";
QImage qimage(100,100,QImage::Format_ARGB32);
qimage.fill(Qt::black);
QPainter painter(&qimage);
paint(&painter);
return qimage;
}
void PainterItem::clearImage()
{
m_linePoints.clear();
update();
}
void PainterItem::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_linePoints.push_back(event->pos());
update();
event->accept();
}
}
void PainterItem::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
m_linePoints.push_back(event->pos());
update();
event->accept();
}
}
void PainterItem::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_linePoints.push_back(event->pos());
update();
event->accept();
}
}