Important: Please read the Qt Code of Conduct -

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

  • 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!

  • Moderators

    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...

  • 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

    @void View::findPossibleMoves(int position)
    if(position % 7 != 6 && isEmpty(position+1))
    if(position % 7 != 0 && isEmpty(position-1))
    if(position > 6 && isEmpty(position-7))
    if(position < 42 && isEmpty(position+7))

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

  • Moderators

    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.

  • @ void View::paintEvent(QPaintEvent event)
    QPainter painter(viewport());
    drawBackground(&painter, sceneRect());
    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++){
    void View::drawBoard(QPainter painter, QRect &rect, int a, bool highlighted)
    QColor lines;
    lines = Qt::yellow;
    lines = QColor(0, 205, 0);
    QBrush brush(lines);
    QPen pen(brush, sceneRect().width()/100);
    QColor fill1(188, 238, 104);
    QColor fill2(110, 139, 61);
    if(a%2 == 0)
    painter->fillRect(rect, fill1);
    painter->fillRect(rect, fill2);
    void View::resizeEvent(QResizeEvent
    }@ ,
    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

    hooray, found this post :
    did viewport()->update() and it works. what is the reason?

  • Moderators

    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().

Log in to reply