Why does my QPainter not draw a point at the coordinates provided by the QMousePressEvent?
-
First off, I am really sorry if this is a dumb question.
I am learning to use QPainter and I decided to make a really basic application which would draw a dot on a QDialog at the points where the user clicks.
The problem is that the QPainter does not draw a point at the clicked location, however it does draw the point if I provide it with hard-coded co-ordinates.
I don't understand why this is happening. Relevant parts of my code are below. I appreciate your help and I'm really sorry if I'm missing something obvious.
PracPaint.h
@
class PracPaint : public QDialog
{
Q_OBJECTpublic:
explicit PracPaint(QWidget *parent = 0);
~PracPaint();protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);private:
Ui::PracPaint *ui;
int x;
int y;
};
@PracPaint.cpp
@
void PracPaint::mousePressEvent(QMouseEvent *mouseEvent)
{
x = mouseEvent->x();
y = mouseEvent->y();
qDebug()<<x<<" "<<y;
}void PracPaint::paintEvent(QPaintEvent *paintEvent)
{
QPainter painter(this);
QPen redPen(Qt::red,10);
redPen.setCapStyle(Qt::RoundCap);
painter.setPen(redPen);
painter.drawPoint(this->x,this->y);
}
@ -
As I see you have checked already that the mouse event is triggered since you are outputting the coordinates. AFAIK you might need a paint event as well, if I understand the logic of your program correctly.
If you assume paintEvent is triggered otherwise, you need check that it is done. -
From the "QWidget::repaint()":/doc/qt-4.8/qwidget.html#repaint docs:
bq. We suggest only using repaint() if you need an immediate repaint, for example during animation. In almost all circumstances update() is better, as it permits Qt to optimize for speed and minimize flicker.
bq. Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.
from the "QWidget::update(":/doc/qt-4.8/qwidget.html#update) docs:
bq. [QWidget::update()] does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.
-
[quote author="mlong" date="1341340887"]From the "QWidget::repaint()":/doc/qt-4.8/qwidget.html#repaint docs:
bq. We suggest only using repaint() if you need an immediate repaint, for example during animation. In almost all circumstances update() is better, as it permits Qt to optimize for speed and minimize flicker.
bq. Warning: If you call repaint() in a function which may itself be called from paintEvent(), you may get infinite recursion. The update() function never causes recursion.
from the "QWidget::update(":/doc/qt-4.8/qwidget.html#update) docs:
bq. [QWidget::update()] does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.[/quote]
Thanks, ~mlong! Now I will use update() wherever it possible
-
Back on topic: I don't think you'll see much happening.
First of all, you completely overrode the painting of the whole dialog. So, perhaps you'll see a small dot, but not much else being painted. Make sure to (in this case: first) call the base implementation:
@
QDialog::paintEvent(paintEvent);
@Then, on the dot you draw itself. I doubt you'll even see that. The problem is: you are painting it right at the mouse cursor. And what is that spot overdrawn with? Exactly: the mouse cursor.
-
[quote author="Andre" date="1341390491"]Back on topic: I don't think you'll see much happening.
First of all, you completely overrode the painting of the whole dialog. So, perhaps you'll see a small dot, but not much else being painted. Make sure to (in this case: first) call the base implementation:
@
QDialog::paintEvent(paintEvent);
@Then, on the dot you draw itself. I doubt you'll even see that. The problem is: you are painting it right at the mouse cursor. And what is that spot overdrawn with? Exactly: the mouse cursor.
[/quote]
Nice analysis Andre[quote author="W.K.S" date="1341338884"]Thanks to both of you.
You're right koahnig - I failed to check that the paintEvent() is actually called and that's where the problem was.Thanks a lot :) [/quote]
Now I am wondering why it might have worked ?