简体   繁体   中英

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

I "solved" that. I discovered the viewport doesn't get updated if you do NOT hover (for example) it with the mouse. So, if you do not interact, viewport does not update. However, viewport does not update if you scroll with the mouse wheel inside the qGraphicsView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM