QGraphicsView/QGraphicsScene Multiple (redundant?) resizeEvent and drawBackground calls during resize



  • I'm getting to many calls to drawBackground during window resize and I'm trying to understand what is causing this behavior. I've created a really short sample program that will reproduce this behavior.
    What I'm seeing is two resizeEvent and four drawBackground calls for each pixel that the window grows during resize with the mouse. Why is this happening and how can I prevent this?
    The scene can contain a rather heavy OpenGL scene and 4 repaints for each resize event is causing a really bad lag during window resize. Now I guess I could setup an internal state and ignore the extra repaints, but I would really like to know why this is happening and if there's a more elegant way of preventing this.
    I'm using Qt 5.1.1 win32 and I've tested on both Win7 and 8 with identical results.

    @
    #include <QApplication>
    #include <QGraphicsView>
    #include <QResizeEvent>
    #include <QDebug>
    #include <QGraphicsScene>
    #include <QGLWidget>

    // CMainView
    class CMainView : public QGraphicsView
    {
    public:
    CMainView() : QGraphicsView()
    {
    setWindowTitle(tr("Simple QGraphicsView Test"));
    }

    ~CMainView() {}
    

    protected:
    virtual void resizeEvent(QResizeEvent* event)
    {
    qDebug() << FUNCTION << "size=" << event->size() << "oldSize=" << event->oldSize();
    if (scene())
    scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
    QGraphicsView::resizeEvent(event);
    }
    };

    // CScene
    class CScene : public QGraphicsScene
    {
    public:
    CScene() : QGraphicsScene() {}
    ~CScene() {}

    virtual void drawBackground(QPainter *painter, const QRectF &rect)
    {
        Q_UNUSED( painter )
        qDebug() &lt;&lt; __FUNCTION__ &lt;&lt; "rect=" << rect;
    }
    

    };

    // main
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
    widget->makeCurrent();
    
    CScene scene;
    
    CMainView view;
    view.setViewport(widget);
    view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    view.setScene(&scene);
    view.show();
    
    return app.exec();
    

    }
    @


  • Moderators

    a resize event always also implies a trigger of a paint event thus you get it.
    This happens event based, meaning if you do a fast resize you will get less paint events than doing it slow.
    This is ok IMHO since it's the desired behavior in most cases and can easily prevented for the special cases (e.g. with the way you already mentioned)



  • I do realize that I will get a repaint after a resize. But what I expect and would like to see is an exact match of one drawBackground for each reziseEvent AND only one reziseEvent for each mouse animation "event".
    In the above example I get 2 resizeEvents for each mouse move and 4 drawBackground calls.
    Removing one line of code from the above example will result in the desired number of events. But not setting a viewport makes the code rather pointless.
    @
    //view.setViewport(viewport);
    @

    So what I'm still trying to understand is why setting a viewport on the QGraphicsView in the code above will change this trace during resize:
    @
    CMainView::resizeEvent size= QSize(213, 98) oldSize= QSize(212, 98)
    CScene::drawBackground rect= QRectF(0,0 213x98)
    @
    To this?
    @
    CMainView::resizeEvent size= QSize(361, 263) oldSize= QSize(360, 263)
    CMainView::resizeEvent size= QSize(361, 263) oldSize= QSize(360, 263)
    CScene::drawBackground rect= QRectF(0,0 361x263)
    CScene::drawBackground rect= QRectF(0,0 361x263)
    CScene::drawBackground rect= QRectF(0,0 361x263)
    CScene::drawBackground rect= QRectF(0,0 361x263)
    @

    As we can see we get two resizeEvent with identical size and oldSize values for each mouse drag. And they result in 4 calls to drawBackground.
    Without a viewport there's only one resizeEvent and drawBackground call for each mouse drag.

    So I'm happy with one drawBackground for each mouse drag, but 4 drawBackground isn't what I would expect.


  • Moderators

    i tried your code.
    It behaves correctly in Qt4. With Qt5 two resize events are sent to the widget. Which seems like a bug to me.
    Maybe you want to "report it":https://bugreports.qt-project.org including your minimal test example.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.