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. QGraphicsView/QGraphicsScene Multiple (redundant?) resizeEvent and drawBackground calls during resize

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

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

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

    }
    @

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

      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)

      --- 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
      • M Offline
        M Offline
        mikag
        wrote on last edited by
        #3

        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.

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

          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.

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