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
|
class QWidgetImage: public QWidget
{
public:
void paintEvent(QPaintEvent *);
void rectangle(Point , Point );
void mousePressEvent(QMouseEvent *);
void mouseReleaseEvent(QMouseEvent *)
private:
QVector<QPainterPath> paths;
}
void QWidgetImage::mousePressEvent(QMouseEvent* event)
{
rectangle(Point(10,10,0), Point(30,30,0));
repaint();
}
void QWidgetImage::rectangle(Point p1, Point p2)
{
QPainterPath path;
path.addRect(p1.getX(), p1.getY(), p2.getX() - p1.getX(), p2.getY() - p1.getY());
paths.push_back(path);
}
void QWidgetImage::QWidgetImage::paintEvent(QPaintEvent * event)
{
QPainter painter(this);
QBrush brush(Qt::blue, Qt::DiagCrossPattern);
painter.setRenderHint(QPainter::Antialiasing);
painter.fillRect(event->rect(), QBrush((Qt::white)));
for (int i = 0; i < paths.size(); i++)
{
painter.fillPath(paths.at(i),brush);
}
} |
Partager