QGraphicsView::NoViewportUpdate doesn't work
-
I have a very simple window with a
QGraphicsView
, aQGraphicsScene
inside, and a simpleQPushButton
. When user clicks button, a line should be added to the scene. However, since I setQGraphicsView::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); }
-
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.
-
@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... -
@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 toupdate()
(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 theQGraphicsView
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.