How does the fonction update() of a derived class work?
-
what I want is simple:
click on a widget and draw a point where we click.
for this, I create a class who inherits the class QWidget and I overwrite the fonction paintEvent and mousePressEvent
and in this class, there is a data QPoint(posMouse) to memorize the position of the mousevoid MyWidget::paintEvent(QPaintEvent *event)
{
//draw a point with a painter according to the posMouse
}void MyWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
posMouse = event->pos();
update(); //doesn't work
}
}my problem is:
i find that the update() in the fonction mousePressEvent doesn't work.
it means that when I click on the MyWidget, I cant see the point. if I want to see it, I must change the focus of the mainwindow. for example I can open another window and click back to my mainwindow of Qt, then I can see the point.
in fact, as we know, the fonction update() is used to call the fonction paintEvent, but here, obviously it doesn't work.I think that this is because the update() here is a fonction of base class, so it call the paintEvent of base class, not my new paintEvent. (Am I right???)
can u help me plz?
thx in advance
-
I don't see any issue in your concept. It is not doing the immediate refresh. You can try calling repaint() instead of update() ?. When you switch between windows it is getting redrawn. Hence you are able to see the point.
-
thx
i ve tried but repaint() doesn't work either.
as u said, when I switch between some windows, it is getting redraw.
so what I want is, when I click, it should be redraw.
but it seems that update or repaint doesnt work....maybe it is because the update or repaint just call the paintEvent of base class as I just said???
[quote author="Dheerendra" date="1389199437"]I don't see any issue in your concept. It is not doing the immediate refresh. You can try calling repaint() instead of update() ?. When you switch between windows it is getting redrawn. Hence you are able to see the point. [/quote]
-
Just try this. I'm drawing circle instead of 'dot'. It works perfectly.
@void Widget::paintEvent(QPaintEvent *event)
{
qDebug() << "Painter is called" << endl;
QPainter painter(this);
painter.setPen(Qt::red);
painter.drawPoint(posMouse.x(),posMouse.y());
painter.drawEllipse(posMouse.x(),posMouse.y(),10,10);
}//draw a point with a painter according to the posMouse
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
qDebug() << "Left Button Clicked" << endl;
posMouse = event->pos();
update(); //works
}
}
@ -
in fact my class inherits QGraphicsView
if we want to use QPainter on it, we must write like this:
QPainter *painter = new QPainter(viewport());maybe this will bring something different?
now I try your code.
[quote author="Dheerendra" date="1389200316"]Just try this. I'm drawing circle instead of 'dot'. It works perfectly.
@void Widget::paintEvent(QPaintEvent *event)
{
qDebug() << "Painter is called" << endl;
QPainter painter(this);
painter.setPen(Qt::red);
painter.drawPoint(posMouse.x(),posMouse.y());
painter.drawEllipse(posMouse.x(),posMouse.y(),10,10);
}//draw a point with a painter according to the posMouse
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
qDebug() << "Left Button Clicked" << endl;
posMouse = event->pos();
update(); //works
}
}
@[/quote] -
I have done a test for ur code.
it works.so I think the problem comes from the QGraphicsView
if nobody knows how to deal with this, maybe I change my base class :)
thx a lot
[quote author="Dheerendra" date="1389200316"]Just try this. I'm drawing circle instead of 'dot'. It works perfectly.
@void Widget::paintEvent(QPaintEvent *event)
{
qDebug() << "Painter is called" << endl;
QPainter painter(this);
painter.setPen(Qt::red);
painter.drawPoint(posMouse.x(),posMouse.y());
painter.drawEllipse(posMouse.x(),posMouse.y(),10,10);
}//draw a point with a painter according to the posMouse
void Widget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
qDebug() << "Left Button Clicked" << endl;
posMouse = event->pos();
update(); //works
}
}
@[/quote]