Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-124618

Incorrect Rendering with Two QGraphicsScenes and an Overlapping QWidget

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15.2
    • Widgets: GraphicsView
    • None
    • macOS

    Description

      I have encountered a rendering issue when two QGraphicsScenes are displayed simultaneously with a QWidget overlaying them. Specifically, the problem arises when the positions of items within both scenes, as well as the overlapping QWidget, are updated concurrently. This results in incorrect rendering for one of the scenes.

      To reproduce this issue, I have prepared a sample code snippet that includes two QGraphicsScenes, each containing a QGraphicsLineItem, and a single QWidget that overlaps both scenes. The bug manifests when the mouse is moved, triggering simultaneous updates to the positions of the items and the QWidget. During this process, one of the scenes consistently exhibits abnormal rendering behavior.

      Here is a brief outline of the steps to reproduce the issue:

      1. Create two QGraphicsScenes and add a QGraphicsLineItem to each.
      2. Overlay a QWidget on top of the scenes.
      3. Set up an event or a mechanism to update the positions of the QGraphicsLineItems and the QWidget in response to mouse movements.
      4. Observe that when the positions are updated, one of the QGraphicsScenes does not render correctly.

      I have attached the code snippet used to replicate this issue for further investigation.

      #include <QMainWindow>
      #include <QWidget>
      #include <QApplication>
      #include <QVBoxLayout>
      #include <QGraphicsView>
      #include <QGraphicsScene>
      #include <QGraphicsLineItem>
      #include <QMouseEvent>
      
      class GraphicsView : public QGraphicsView {
      public:
          GraphicsView(QWidget *parent = nullptr) : QGraphicsView(parent) {
              setAttribute(Qt::WA_TransparentForMouseEvents);
              QGraphicsScene *scene = new QGraphicsScene(this);
              setScene(scene);
              setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
              setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
              line = new QGraphicsLineItem();
              line->setPen(QPen(Qt::red));
              scene->addItem(line);
          }
          
          void resizeEvent(QResizeEvent *event) override {
              scene()->setSceneRect(0, 0, width(), height());
          }
          
          void moveLine(double xPos) {
              line->setLine(xPos, 0, xPos, height());
          }
      
          QGraphicsLineItem *line = nullptr;
      };
      
      class Widget : public QWidget {
      public:
          Widget(QWidget *parent = nullptr) : QWidget(parent) {
              setMouseTracking(true);
              view1 = new GraphicsView(this);
              view2 = new GraphicsView(this);
              QVBoxLayout *layout = new QVBoxLayout(this);
              layout->addWidget(view1);
              layout->addWidget(view2);
      
              widget = new QWidget(this);
              widget->setAttribute(Qt::WA_TransparentForMouseEvents);
              widget->setStyleSheet("background-color: #80808080;");
              widget->show();
              widget->resize(100, 100);
              widget->raise();
          }
      
          void mouseMoveEvent(QMouseEvent *event) override {
              view1->moveLine(view1->mapFromParent(event->pos()).x());
              view2->moveLine(view2->mapFromParent(event->pos()).x());
              widget->move(event->pos().x(), event->pos().y());
          }
      
          QWidget *widget = nullptr;
          GraphicsView *view1 = nullptr;
          GraphicsView *view2 = nullptr;
      };
      
      int main(int argc, char *argv[]) {
          QApplication a(argc, argv);
          QMainWindow window;
          window.setCentralWidget(new Widget());
          window.resize(800, 800);
          window.show();
          return a.exec();
      } 

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            bibr Andreas Aardal Hanssen
            ruiyuanhu erika haha
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes