{
QList<QPoint> current_pos;
public:
explicit Label
(QWidget * parent
= 0);
protected:
};
, size(100, 100)
{
rect.
setTopLeft(QPoint(100,
100));
rect.setSize(size);
setGeometry(rect);
}
{
qreal sx = event->rect().width() / (qreal)size.width();
qreal sy = event->rect().height() / (qreal)size.height();
painter.scale(sx, sy); // scale the painter
QPen pen
(Qt
::green,
4, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
painter.setPen(pen);
foreach
(QPoint point, current_pos
) painter.drawPoint(point);
}
{
qreal sx = rect().width() / (qreal)size.width();
qreal sy = rect().height() / (qreal)size.height();
p.setX(event->pos().x() / sx); // Scale to original before storing
p.setY(event->pos().y() / sy);
current_pos << p;
this->repaint();
}
int main(int argc, char ** argv)
{
Label l;
l.show();
return app.exec();
}
class Label : public QLabel
{
QList<QPoint> current_pos;
QSize size;
public:
explicit Label(QWidget * parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
};
Label::Label(QWidget * parent)
: QLabel(parent)
, size(100, 100)
{
QRect rect;
rect.setTopLeft(QPoint(100, 100));
rect.setSize(size);
setGeometry(rect);
}
void Label::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
QPainter painter(this);
qreal sx = event->rect().width() / (qreal)size.width();
qreal sy = event->rect().height() / (qreal)size.height();
painter.scale(sx, sy); // scale the painter
QPen pen(Qt::green, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
foreach(QPoint point, current_pos)
painter.drawPoint(point);
}
void Label::mouseMoveEvent(QMouseEvent *event)
{
qreal sx = rect().width() / (qreal)size.width();
qreal sy = rect().height() / (qreal)size.height();
QPoint p;
p.setX(event->pos().x() / sx); // Scale to original before storing
p.setY(event->pos().y() / sy);
current_pos << p;
this->repaint();
}
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
Label l;
l.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks