PDA

View Full Version : How can we call the QPainter under QMouseEvent



cemshit
5th February 2010, 17:50
hi all,

I have a problem. I need to draw circles when I click the mainwindow.Therefore the code of the drawing circles should be under the mouseevent function, isn't it? However, I couldn't do it. It give error says "Painter not active". Can anybody help on this issue?

Coises
5th February 2010, 18:42
I need to draw circles when I click the mainwindow.Therefore the code of the drawing circles should be under the mouseevent function, isn't it?

Code for drawing virtually always goes in QWidget::paintEvent.

In working with GUI systems, remember that anything drawn can need to be redrawn at any time — say, because a window from another application comes to the front, overwrites your window, and is then removed. So it’s not like you’ll be drawing this circle just once, when the mouse clicks; you must be prepared to draw it upon request (that is, whenever QWidget::paintEvent is called) for as long as the circle should be displayed.

You will need some data structure within your mainwindow that maintains the information needed to draw the circle (or circles, if existing ones are to stay visible as new ones are added). That information is what you update in QWidget::mousePressEvent or QWidget::mouseReleaseEvent, after which you call QWidget::update to tell Qt to repaint the window. In QWidget::paintEvent you draw the circle(s) indicated in your data structure, regardless of how QWidget::paintEvent came to be called.

cemshit
5th February 2010, 19:29
Point clickedPoint;


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);
}

MainWindow::~MainWindow()
{
}


void MainWindow::mousePressEvent( QMouseEvent *event)
{

if( frame1->underMouse() && event->x() > 60){
qDebug() << "global Pos: " << event->globalPos();
qDebug() << "frame glob: " << frame1->mapFromGlobal( event->globalPos() );

clickedPoint=event->pos();
QWidget::mousePressEvent(event);

}
QMainWindow::mousePressEvent( event );
}

void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);

painter.setRenderHint(QPainter::Antialiasing);


painter.setBrush(Qt::white);
//painter.setPen(QPen(QBrush("#575555"), 1));
painter.drawRect(10, 10, 680, 500);

QWidget::paintEvent(event);

}


first of all thanks for your respond in the forum. My code is above. What sould I add the code to draw circles where I press every time. As you say I want to keep previous circles and add new ones with clicking where I want to put them.

Coises
5th February 2010, 19:48
Add to your definition of class MainWindow (probably in the mainwindow.h file) a data structure to hold the points at which you have clicked. I suggest using a QList<QPoint> for that. In mousePressEvent (http://doc.trolltech.com/latest/qwidget.html#mousePressEvent), add the point where you clicked to the list and call update() (http://doc.trolltech.com/latest/qwidget.html#update). Then, in paintEvent (http://doc.trolltech.com/latest/qwidget.html#paintEvent), iterate through your list drawing each circle.