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. How does the fonction update() of a derived class work?

How does the fonction update() of a derived class work?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.7k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    bear234
    wrote on last edited by
    #1

    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 mouse

    void 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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bear234
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          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
          }
          }
          @

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bear234
            wrote on last edited by
            #5

            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]

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bear234
              wrote on last edited by
              #6

              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]

              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