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. Update() doesn't start paintevent until the window is resized

Update() doesn't start paintevent until the window is resized

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 7.2k 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.
  • Z Offline
    Z Offline
    zurbek
    wrote on last edited by
    #1

    Hi there!
    I have a function in which i call update(), I want it to redraw the things in my qGraphicsView. Now, the thing is that it doesn't redraw anything unless the window is resized. It somehow puts on the stack all the calls of update() and starts them when I resize the screen. Why is that?
    Thanks for your help!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      Do you filter update events somewhere? Update events should paint your widget in the next iteration of the event-loop, so they don't "get on the stack"...
      but without some code we can just guess what you are doing wrong...

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zurbek
        wrote on last edited by
        #3

        Here's the part of code responsible for it :
        @void View::mousePressEvent(QMouseEvent event)
        {
        int xPosition = (int)(event->x()/(sceneRect().width()/7));
        int yPosition = (int)(event->y()/(sceneRect().height()/7));
        int position = xPosition + yPosition
        7;
        findPossibleMoves(position);
        }@

        @void View::findPossibleMoves(int position)
        {
        highlights->clear();
        if(position % 7 != 6 && isEmpty(position+1))
        highlights->push_back(position+1);
        if(position % 7 != 0 && isEmpty(position-1))
        highlights->push_back(position-1);
        if(position > 6 && isEmpty(position-7))
        highlights->push_back(position-7);
        if(position < 42 && isEmpty(position+7))
        highlights->push_back(position+7);
        update();
        }@

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zurbek
          wrote on last edited by
          #4

          still got this issue, any ideas? cant seem to fix it

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            you need to post more code (eventFilters, paintEvent handler, initializations,...) ... the posted code is obviously correct.
            There must be something else which prevents the delivery of paintEvents, etc.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zurbek
              wrote on last edited by
              #6

              @ void View::paintEvent(QPaintEvent event)
              {
              QPainter painter(viewport());
              drawBackground(&painter, sceneRect());
              drawTiles();
              }@
              @
              void View::drawBackground(QPainter painter, const QRectF &rect)
              {
              for(int i = 0; i < 49; i++){
              QRect square(sceneRect().width()/7
              (i%7), sceneRect().height()/7
              (i/7), sceneRect().width()/7, sceneRect().height()/7);
              drawBoard(painter, square, i, false);
              }
              for(int i = 0; i < highlights->size(); i++){
              int position = highlights->at(i);
              QRect square(sceneRect().width()/7*(position%7), sceneRect().height()/7*(position/7), sceneRect().width()/7, sceneRect().height()/7);
              drawBoard(painter, square, position, true);
              }
              }@
              @
              void View::drawTiles()
              {
              for(int i = 0; i < 14; i++){
              tiles[i]->draw();
              }
              }@
              @
              void View::drawBoard(QPainter painter, QRect &rect, int a, bool highlighted)
              {
              QColor lines;
              if(highlighted)
              lines = Qt::yellow;
              else
              lines = QColor(0, 205, 0);
              QBrush brush(lines);
              QPen pen(brush, sceneRect().width()/100);
              QColor fill1(188, 238, 104);
              QColor fill2(110, 139, 61);
              painter->setPen(pen);
              painter->drawRect(rect);
              if(a%2 == 0)
              painter->fillRect(rect, fill1);
              else
              painter->fillRect(rect, fill2);
              }@
              @
              void View::resizeEvent(QResizeEvent
              event)
              {
              this->setSceneRect(0,0,this->frameSize().width(),this->frameSize().height());
              }@ ,
              where View derives from QGraphicsView

              so it works like this :

              mousePressEvent() -> finds possible moves & puts them in highlights -> calls update()

              update is supposed to redraw but it doesnt unless i resize the window

              by the looks of it, it doesnt even put the update() calls on stack, it just doesnt redraw anything. its window resize that calls the paintevent

              EDIT:
              hooray, found this post : http://stackoverflow.com/questions/2274512/qt4-update-or-repaint-fails-to-trigger-paintevent
              did viewport()->update() and it works. what is the reason?

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                you shouldn't use the paintEvent() like you did!

                you are starting a painter not on the widget itself but on a complete other widget (viewport widget). I think this will result in a warning in the console?! And all following paint calls will fail...

                Additionally you override QGraphicsView::drawBackground() which is virtual and gets called by Qt on resize of the viewport. This is why it works on resizing and not when calling your update() on the QGraphicsView widget.

                You should completely remove your current paintEvent override and as you already noticed use viewport->update().

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                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