359
+ − 1
#include <QPainter>
+ − 2
#include <QPen>
+ − 3
#include "pixlabel.h"
+ − 4
+ − 5
PixLabel::PixLabel()
+ − 6
: QLabel(0)
+ − 7
{
+ − 8
+ − 9
}
+ − 10
+ − 11
void PixLabel::paintEvent(QPaintEvent * event)
+ − 12
{
+ − 13
QLabel::paintEvent(event);
+ − 14
QPainter p(this);
+ − 15
+ − 16
p.fillRect(QRect(0, 0, 1024, 512), QBrush(Qt::black));
+ − 17
+ − 18
if (rects.size())
+ − 19
{
361
+ − 20
p.setPen(QPen(Qt::lightGray));
+ − 21
QVector<QPoint> centers;
+ − 22
for(QList<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it)
+ − 23
centers.push_back((*it).center());
+ − 24
p.drawPolyline(QPolygon(centers));
+ − 25
+ − 26
p.setPen(QPen(Qt::white));
+ − 27
p.drawRects(rects.toVector());
+ − 28
359
+ − 29
p.setPen(QPen(Qt::yellow));
+ − 30
p.drawRect(rects.last());
+ − 31
}
+ − 32
}
+ − 33
+ − 34
void PixLabel::mousePressEvent(QMouseEvent * e)
+ − 35
{
+ − 36
if (!rects.empty())
+ − 37
{
+ − 38
if (e->button() == Qt::LeftButton)
+ − 39
rects[rects.size() - 1].moveTopLeft(QPoint(e->x(), e->y()));
+ − 40
else
+ − 41
if (e->button() == Qt::RightButton)
+ − 42
rects[rects.size() - 1].setBottomRight(QPoint(e->x(), e->y()));
+ − 43
repaint();
+ − 44
}
+ − 45
}
+ − 46
+ − 47
void PixLabel::AddRect()
+ − 48
{
+ − 49
rects.push_back(QRect(0, 0, 1, 1));
+ − 50
repaint();
+ − 51
}