QGraphicsView::NoViewportUpdate doesn't work



  • 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);
    }
    

  • Qt Champions 2016

    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.



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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