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. Why does calling sceneRect() cause process crash?
Qt 6.11 is out! See what's new in the release blog

Why does calling sceneRect() cause process crash?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 919 Views 1 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.
  • M Offline
    M Offline
    Mbkerdellou
    wrote on last edited by
    #1

    I am experimenting with Qt graphics view, and to understand how things work i have this simple code

    #include "MainWindow.h"
    #include <QGraphicsPixmapItem>
    #include <QGraphicsView>
    #include <QtWidgets>
    #include "TileItem.h"
    MainWindow::MainWindow(QWidget* parent)
        : QMainWindow(parent) {
        QWidget* centralWidget = new QWidget(this);
        QHBoxLayout* centralLayout = new QHBoxLayout();
        centralWidget->setLayout(centralLayout);
        loadDefaultTiles();
        centralLayout->addWidget(m_gameView);
        m_gameView->setBackgroundBrush(Qt::red);
        centralLayout->addStretch(10);
        setCentralWidget(centralWidget);
    }
    
    void MainWindow::loadDefaultTiles() {
        m_gameScene = new QGraphicsScene();
        m_gameView = new QGraphicsView();
        m_gameView->setFrameStyle(QFrame::NoFrame);
    
        m_gameView->setScene(m_gameScene);
    
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                TileItem* tile = new TileItem();
                m_gameScene->addItem(tile);
                tile->setPos(i * 50, j * 50);
            }
        }
    
        m_gameView->setMinimumWidth(m_gameScene->width());
        m_gameView->setMinimumHeight(m_gameScene->height());
    
        qDebug() << m_gameScene->items().count();
    }
    

    and

    #include "TileItem.h"
    #include <QtGui/qpainter.h>
    #include <QtWidgets/qgraphicsscene.h>
    
    TileItem::TileItem() {}
    
    
    QRectF TileItem::boundingRect() const {
        if (scene()) {
            qDebug() << scene()->sceneRect();
        }
        return {0, 0, 50, 50};
    }
    void TileItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
        painter->drawPixmap(boundingRect(), m_texture, m_texture.rect());
    }
    
    

    But when I run it, the program crashes instantly. Whats weird to me is that the same exact code but changing qDebug() << scene()->sceneRect(); to qDebug() << scene()->parent(); works fine.

    Whats happening? sceneRect() always returns something, its not returning void if no scene rect is set.

    JonBJ 1 Reply Last reply
    0
    • M Mbkerdellou

      I am experimenting with Qt graphics view, and to understand how things work i have this simple code

      #include "MainWindow.h"
      #include <QGraphicsPixmapItem>
      #include <QGraphicsView>
      #include <QtWidgets>
      #include "TileItem.h"
      MainWindow::MainWindow(QWidget* parent)
          : QMainWindow(parent) {
          QWidget* centralWidget = new QWidget(this);
          QHBoxLayout* centralLayout = new QHBoxLayout();
          centralWidget->setLayout(centralLayout);
          loadDefaultTiles();
          centralLayout->addWidget(m_gameView);
          m_gameView->setBackgroundBrush(Qt::red);
          centralLayout->addStretch(10);
          setCentralWidget(centralWidget);
      }
      
      void MainWindow::loadDefaultTiles() {
          m_gameScene = new QGraphicsScene();
          m_gameView = new QGraphicsView();
          m_gameView->setFrameStyle(QFrame::NoFrame);
      
          m_gameView->setScene(m_gameScene);
      
          for (int i = 0; i < 10; i++) {
              for (int j = 0; j < 10; j++) {
                  TileItem* tile = new TileItem();
                  m_gameScene->addItem(tile);
                  tile->setPos(i * 50, j * 50);
              }
          }
      
          m_gameView->setMinimumWidth(m_gameScene->width());
          m_gameView->setMinimumHeight(m_gameScene->height());
      
          qDebug() << m_gameScene->items().count();
      }
      

      and

      #include "TileItem.h"
      #include <QtGui/qpainter.h>
      #include <QtWidgets/qgraphicsscene.h>
      
      TileItem::TileItem() {}
      
      
      QRectF TileItem::boundingRect() const {
          if (scene()) {
              qDebug() << scene()->sceneRect();
          }
          return {0, 0, 50, 50};
      }
      void TileItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) {
          painter->drawPixmap(boundingRect(), m_texture, m_texture.rect());
      }
      
      

      But when I run it, the program crashes instantly. Whats weird to me is that the same exact code but changing qDebug() << scene()->sceneRect(); to qDebug() << scene()->parent(); works fine.

      Whats happening? sceneRect() always returns something, its not returning void if no scene rect is set.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mbkerdellou said in Why does calling sceneRect() cause process crash?:

      loadDefaultTiles();
      centralLayout->addWidget(m_gameView);
      

      Does it make a difference if you swap the order of these 2 lines?

      If you move the qDebug() << scene()->sceneRect(); to, say, the paint() does it crash there?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        What is the backtrace of your crash ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Mbkerdellou
          wrote on last edited by
          #4

          @JonB No swapping the order does not fix it. And moving it to paint does not cause it to crash,

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mbkerdellou
            wrote on last edited by
            #5

            @SGaist
            Image 10.02.26 at 21.52.png

            JonBJ 1 Reply Last reply
            0
            • M Mbkerdellou

              @SGaist
              Image 10.02.26 at 21.52.png

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Mbkerdellou
              Assuming that backtrace continues on & on with same calls, you seem to be stuck in a loop where the sceneRect() calculation needs to ask for the item's boundingRect(), and then you try to access the sceneRect() from there. Infinite recursion I assume. You'll need to call it from somewhere else. or maybe it becomes safer when the size has settled down.

              M 1 Reply Last reply
              4
              • JonBJ JonB

                @Mbkerdellou
                Assuming that backtrace continues on & on with same calls, you seem to be stuck in a loop where the sceneRect() calculation needs to ask for the item's boundingRect(), and then you try to access the sceneRect() from there. Infinite recursion I assume. You'll need to call it from somewhere else. or maybe it becomes safer when the size has settled down.

                M Offline
                M Offline
                Mbkerdellou
                wrote on last edited by
                #7

                @JonB Thank you, makes sense

                1 Reply Last reply
                0

                • Login

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