Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does my QPainter not draw a point at the coordinates provided by the QMousePressEvent?
QtWS25 Last Chance

Why does my QPainter not draw a point at the coordinates provided by the QMousePressEvent?

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 7.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    W.K.S
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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);
    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Try calling @update();@ at the end of your mousePressEvent.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • W Offline
          W Offline
          W.K.S
          wrote on last edited by
          #4

          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 :)

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tucnak
            wrote on last edited by
            #5

            [quote author="mlong" date="1341338061"]Try calling @update();@ at the end of your mousePressEvent.[/quote]

            Hi, ~mlong!

            Why not to use repaint() slot? I am using it always and I don't know difference between them.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              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.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tucnak
                wrote on last edited by
                #7

                [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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    [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 ?

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved