PDA

View Full Version : paint with mouse



Vincenzo
19th October 2008, 22:15
I want paint some circle by press the mouse button.


void GraphArea::mousePressEvent1(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
show = true;
}
}

void GraphArea::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
if (show)
paintCircle1(painter);
}


It works...
...but my plan/problem is: if i press mouse button 2nd time, then paint 2nd circle, to third press clear the whole widget - I don't know how to do it!

PS.: sorry for my english...

wysota
19th October 2008, 22:31
Store data about the circles and access it during next mousepress.


void xxx::mousePressEvent(QMouseEvent *me){
// assuming QVector<QPair<QPoint, int> > circles is a member variable of the class
switch(circles.count()){
case 0: case 1: circles << qMakePair(me->pos(), 30); break;
case 2: circles.clear();
}
update();
}
void xxx::paintEvent(QPaintEvent *pe){
QPainter painter(this);
for(int i=0;i<circles.count();i++){
painter.drawEllipse(circles[i].first, circles[i].second, circles[i].second);
}
}

jacek
19th October 2008, 23:05
You have three states: no circles, one circle, two circles.

So you need some variable that will hold the current state and then you have change this state accordingly in mousePressEvent() and paint the circles in paintEvent().