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::NoViewportUpdate doesn't work
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView::NoViewportUpdate doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsviewqgraphicssceneviewportqt5.5
8 Posts 2 Posters 4.2k Views 2 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.
  • A Offline
    A Offline
    alogim
    wrote on last edited by
    #1

    I have a very simple window with a QGraphicsView, a QGraphicsScene inside, and a simple QPushButton. When user clicks button, a line should be added to the scene. However, since I set QGraphicsView::NoViewportUpdate, the line shouldn't be displayed. On the opposite, the line gets displayed.
    According to the documentation, QGraphicsView will never update its viewport when the scene changes; the user is expected to control all updates. This mode disables all (potentially slow) item visibility testing in QGraphicsView, and is suitable for scenes that either require a fixed frame rate, or where the viewport is otherwise updated externally.

    How do I solve this problem?

    Here is the code:
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QGraphicsScene>
    #include <QGraphicsView>
    #include <QWidget>
    #include <QPushButton>
    
    class MainWindow : public QWidget
    {
    		Q_OBJECT
    
    	public:
    		MainWindow(QWidget *parent = 0);
    		~MainWindow();
    
    	private:
    		QGraphicsView* view;
    		QGraphicsScene* scene;
    		QPushButton* b;
    
    	public slots:
    		void start();
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    
    #include <QVBoxLayout>
    
    MainWindow::MainWindow(QWidget *parent)
    	: QWidget(parent)
    {
    	scene = new QGraphicsScene(0, 0, 400, 400);
    	view = new QGraphicsView(scene);
    	view->setViewportUpdateMode(QGraphicsView::NoViewportUpdate);
    
    	b = new QPushButton("Start");
    	connect (b, &QPushButton::clicked, this, &MainWindow::start);
    
    	QVBoxLayout* layout = new QVBoxLayout;
    	layout->addWidget(view);
    	layout->addWidget(b);
    	setLayout(layout);
    }
    
    MainWindow::~MainWindow()
    {
    }
    
    void MainWindow::start()
    {
    	scene->addLine(0, 0, 200, 200);
    }
    
    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Hello,
      I've tested your code (from the repository you provided in the other thread) and it the flag works as expected. You only get the view updated when a paint event is triggered from the window system (i.e. when resizing the widget). If you wish to stop the updates altogether you should filter out the events yourself (by installing an event filter for example) and issue the painting manually.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      A 1 Reply Last reply
      0
      • kshegunovK kshegunov

        Hello,
        I've tested your code (from the repository you provided in the other thread) and it the flag works as expected. You only get the view updated when a paint event is triggered from the window system (i.e. when resizing the widget). If you wish to stop the updates altogether you should filter out the events yourself (by installing an event filter for example) and issue the painting manually.

        Kind regards.

        A Offline
        A Offline
        alogim
        wrote on last edited by
        #3

        @kshegunov Ok, thank you, now it's clear, I was confused since the documentation states that updates do not occur at all.

        kshegunovK 1 Reply Last reply
        0
        • A alogim

          @kshegunov Ok, thank you, now it's clear, I was confused since the documentation states that updates do not occur at all.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @alogim
          Hello,
          Actually it states that the viewport will not be updated when the scene changes, not that it will not repaint itself when it's resized. :)

          QGraphicsView will never update its viewport when the scene changes;

          Kind regards.

          Read and abide by the Qt Code of Conduct

          A 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @alogim
            Hello,
            Actually it states that the viewport will not be updated when the scene changes, not that it will not repaint itself when it's resized. :)

            QGraphicsView will never update its viewport when the scene changes;

            Kind regards.

            A Offline
            A Offline
            alogim
            wrote on last edited by
            #5

            @kshegunov Actually it states that the viewport will not be updated when the scene changes, not that it will not repaint itself when it's resized. :)
            Exactly: so if I change the scene by adding an element, that element shouldn't be displayed.
            Instead, the element is displayed plus, if I resize the view or move the view's scrollbar, after a while it gets updated, and that's strange. It seems pretty useless...

            kshegunovK 1 Reply Last reply
            0
            • A alogim

              @kshegunov Actually it states that the viewport will not be updated when the scene changes, not that it will not repaint itself when it's resized. :)
              Exactly: so if I change the scene by adding an element, that element shouldn't be displayed.
              Instead, the element is displayed plus, if I resize the view or move the view's scrollbar, after a while it gets updated, and that's strange. It seems pretty useless...

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @alogim
              I've actually tested this (Debian 4.3 kernel, Qt 5.5.1) and I don't get the scene viewport updated. What I did is to comment out your call to update() (in your source). When I start the application I see nothing. The view is updated with the current state of the scene when my mouse enters/leaves the QGraphicsView or when I resize/move the top level widget. If I do nothing I see nothing to change. It's possible this might be a bug (if you're using another version and it was subsequently fixed), but it works fine for me.

              Read and abide by the Qt Code of Conduct

              A 1 Reply Last reply
              1
              • kshegunovK kshegunov

                @alogim
                I've actually tested this (Debian 4.3 kernel, Qt 5.5.1) and I don't get the scene viewport updated. What I did is to comment out your call to update() (in your source). When I start the application I see nothing. The view is updated with the current state of the scene when my mouse enters/leaves the QGraphicsView or when I resize/move the top level widget. If I do nothing I see nothing to change. It's possible this might be a bug (if you're using another version and it was subsequently fixed), but it works fine for me.

                A Offline
                A Offline
                alogim
                wrote on last edited by
                #7

                @kshegunov Right, right, you're right. Until you hover the view with the mouse, it doesn't get updated.
                I'm on Arch, kernel 4.3.3, Qt 5.5.1.
                So, if I install an eventFilter for the viewport perhaps I can compeltely prevent updates.

                kshegunovK 1 Reply Last reply
                0
                • A alogim

                  @kshegunov Right, right, you're right. Until you hover the view with the mouse, it doesn't get updated.
                  I'm on Arch, kernel 4.3.3, Qt 5.5.1.
                  So, if I install an eventFilter for the viewport perhaps I can compeltely prevent updates.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @alogim
                  Yes, you can, but then you'll need at some point to update the scene's viewport (the QSceneView) manually and this might have some undesirable effects when you move/resize your widget(s).

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved