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. QGraphicsScene does not fit QGraphicsView full size
Forum Updated to NodeBB v4.3 + New Features

QGraphicsScene does not fit QGraphicsView full size

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 6.4k Views
  • 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.
  • JonBJ JonB

    @odelaune
    That means your scene is smaller than the view. I think you want void QGraphicsView::fitInView(const QGraphicsItem *item, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio) ?

    Ah, actually for the whole scene: void QGraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio)

    ui->view->fitInView( scene->sceneRect(),Qt::KeepAspectRatio);
    

    ?

    O Offline
    O Offline
    odelaune
    wrote on last edited by odelaune
    #3

    @JonB
    I agree that my scene is smaller than my view but I do not understand why QGraphicsView::fitInView does not do the job.
    In test.cpp, using "m_view->fitInView (scene->sceneRect(),Qt::IgnoreAspectRatio);" instead of "m_view->fitInView(item, Qt::IgnoreAspectRatio);" gives me exactly the same result, i.e. the screenshot I have given in my first post.

    JonBJ 1 Reply Last reply
    0
    • O odelaune

      @JonB
      I agree that my scene is smaller than my view but I do not understand why QGraphicsView::fitInView does not do the job.
      In test.cpp, using "m_view->fitInView (scene->sceneRect(),Qt::IgnoreAspectRatio);" instead of "m_view->fitInView(item, Qt::IgnoreAspectRatio);" gives me exactly the same result, i.e. the screenshot I have given in my first post.

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

      @odelaune
      The red square is the whole QMainWindow, right? Why would you expect the QGraphicsView to occupy the whole of that, when it's only the centralWidget? See the picture at https://doc.qt.io/qt-6/qmainwindow.html#qt-main-window-framework. I wouldn't use a QMainWindow if I wanted one widget to occupy the whole window.

      O 1 Reply Last reply
      0
      • JonBJ JonB

        @odelaune
        The red square is the whole QMainWindow, right? Why would you expect the QGraphicsView to occupy the whole of that, when it's only the centralWidget? See the picture at https://doc.qt.io/qt-6/qmainwindow.html#qt-main-window-framework. I wouldn't use a QMainWindow if I wanted one widget to occupy the whole window.

        O Offline
        O Offline
        odelaune
        wrote on last edited by odelaune
        #5

        The red square is drawn using "m_view->setStyleSheet("border: 5px solid red");" so technically this is the border of the QGraphicsView object and indeed this object is defined as the central widget. So it is still not clear to me why the QGraphicsScene does not fit the QGraphicsView.
        Since I have no toolbars, no dock, etc. (I have only one widget in my mainwindow, defined as the central widget). So everything should scale the full size of the mainwindow, but it is not.

        How would you rewrite my code to get what I look for?

        JonBJ 1 Reply Last reply
        0
        • O odelaune

          The red square is drawn using "m_view->setStyleSheet("border: 5px solid red");" so technically this is the border of the QGraphicsView object and indeed this object is defined as the central widget. So it is still not clear to me why the QGraphicsScene does not fit the QGraphicsView.
          Since I have no toolbars, no dock, etc. (I have only one widget in my mainwindow, defined as the central widget). So everything should scale the full size of the mainwindow, but it is not.

          How would you rewrite my code to get what I look for?

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

          @odelaune

          • I would try with a QWidget instead of a QMainWindow.
          • Maybe your scene is already the full size of the view (or vice versa). Maybe you actually need to enlarge the scene/view to fill the whole window.

          That's all i know.

          O 1 Reply Last reply
          0
          • JonBJ JonB

            @odelaune

            • I would try with a QWidget instead of a QMainWindow.
            • Maybe your scene is already the full size of the view (or vice versa). Maybe you actually need to enlarge the scene/view to fill the whole window.

            That's all i know.

            O Offline
            O Offline
            odelaune
            wrote on last edited by odelaune
            #7

            Actually this code is a sample of my whole code where the QGraphicsView is inside a widget, so I experience the same issue either with QMainWindow or with QWidget.

            @JonB said in QGraphicsScene does not fit QGraphicsView full size:

            • Maybe your scene is already the full size of the view (or vice versa). Maybe you actually need to enlarge the scene/view to fill the whole window.

            Why not but I am not sure to know how to do it.

            1 Reply Last reply
            0
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #8

              @odelaune said in QGraphicsScene does not fit QGraphicsView full size:

              m_view = new QGraphicsView();

              m_view = new QGraphicsView( this );

              QGraphicsScene *scene = new QGraphicsScene( m_view or this );
              m_view->setRenderHint(QPainter::Antialiasing, true);
              m_view->setScene(scene); ///The view does not take ownership of scene with this call.

              Try to change resize policy of scene

              check this example: https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/graphicsview/elasticnodes?h=6.3

              1 Reply Last reply
              0
              • O Offline
                O Offline
                odelaune
                wrote on last edited by odelaune
                #9

                Hmmm, here is my code now

                main.cpp

                // Qt headers
                #include <QApplication>
                
                #include "test.h"
                
                int main(int argc, char *argv[])
                {
                    QApplication application(argc, argv);
                
                    Test *applicationWindow = new Test();
                    applicationWindow->setMinimumWidth(400);
                    applicationWindow->setMinimumHeight(400);
                    applicationWindow->show();
                
                    return application.exec();
                }
                

                test.cpp

                // Qt headers
                #include <QPixmap>
                #include <QGraphicsPixmapItem>
                #include <QGraphicsRectItem>
                #include <QPainter>
                #include <QPen>
                #include <QResizeEvent>
                
                // Other headers
                #include "test.h"
                
                Test::Test(QWidget* parent /*=nullptr*/):
                    QMainWindow(parent)
                {
                    m_view = new QGraphicsView(this);
                
                    // draw border of the QGraphicsView
                    m_view->setStyleSheet("border: 5px solid red");
                
                    m_scene = new QGraphicsScene(m_view);
                
                    m_view->setRenderHint(QPainter::Antialiasing, true);
                    m_view->setScene(m_scene);
                    m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                    m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                
                    QPixmap img("tmp.svg");
                    QGraphicsPixmapItem *item = m_scene->addPixmap(img);
                
                    // draw border of the QGraphicsScene
                    QGraphicsRectItem *rect_item = m_scene->addRect(m_scene->sceneRect());
                    rect_item->setParentItem(item);
                    QPen pen = QPen(Qt::black);
                    pen.setWidth(10);
                    rect_item->setPen(pen);
                
                    // set the mapView as the central widget
                    setCentralWidget(m_view);
                
                    m_view->show();
                
                    m_view->fitInView (m_scene->itemsBoundingRect(),Qt::IgnoreAspectRatio); // useless here
                
                }
                
                // destructor
                Test::~Test()
                {
                }
                
                
                void Test::resizeEvent(QResizeEvent* event)
                {
                    if(!m_scene)
                        return;
                    QRectF bounds = m_scene->itemsBoundingRect();
                    m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                }
                

                test.h

                #ifndef TEST_H
                #define TEST_H
                
                #include <QMainWindow>
                #include <QGraphicsView>
                #include <QGraphicsScene>
                
                class Test : public QMainWindow
                {
                    Q_OBJECT
                public:
                    explicit Test(QWidget* parent = nullptr);
                    ~Test() override;
                    void resizeEvent(QResizeEvent* event) override;
                
                public slots:
                
                private:
                    QGraphicsView *m_view;
                    QGraphicsScene *m_scene;
                };
                
                #endif // TEST_H
                

                And here is what I get when I start the application:
                Capture d’écran du 2022-06-22 10-27-39.png
                So clearly not what I am expecting.

                However, I get what I want when I resize the window:
                Capture d’écran du 2022-06-22 10-27-49.png

                The trick is done via the resizeEvent method. But I do not understand how can I do the same in the same in the constructor. Indeed

                m_view->fitInView (m_scene->itemsBoundingRect(),Qt::IgnoreAspectRatio)
                

                does not do the job when it is placed at the end of the constructor but works fine in resizeEvent.

                Any idea?

                JonBJ 1 Reply Last reply
                0
                • O odelaune

                  Hmmm, here is my code now

                  main.cpp

                  // Qt headers
                  #include <QApplication>
                  
                  #include "test.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication application(argc, argv);
                  
                      Test *applicationWindow = new Test();
                      applicationWindow->setMinimumWidth(400);
                      applicationWindow->setMinimumHeight(400);
                      applicationWindow->show();
                  
                      return application.exec();
                  }
                  

                  test.cpp

                  // Qt headers
                  #include <QPixmap>
                  #include <QGraphicsPixmapItem>
                  #include <QGraphicsRectItem>
                  #include <QPainter>
                  #include <QPen>
                  #include <QResizeEvent>
                  
                  // Other headers
                  #include "test.h"
                  
                  Test::Test(QWidget* parent /*=nullptr*/):
                      QMainWindow(parent)
                  {
                      m_view = new QGraphicsView(this);
                  
                      // draw border of the QGraphicsView
                      m_view->setStyleSheet("border: 5px solid red");
                  
                      m_scene = new QGraphicsScene(m_view);
                  
                      m_view->setRenderHint(QPainter::Antialiasing, true);
                      m_view->setScene(m_scene);
                      m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                      m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                  
                      QPixmap img("tmp.svg");
                      QGraphicsPixmapItem *item = m_scene->addPixmap(img);
                  
                      // draw border of the QGraphicsScene
                      QGraphicsRectItem *rect_item = m_scene->addRect(m_scene->sceneRect());
                      rect_item->setParentItem(item);
                      QPen pen = QPen(Qt::black);
                      pen.setWidth(10);
                      rect_item->setPen(pen);
                  
                      // set the mapView as the central widget
                      setCentralWidget(m_view);
                  
                      m_view->show();
                  
                      m_view->fitInView (m_scene->itemsBoundingRect(),Qt::IgnoreAspectRatio); // useless here
                  
                  }
                  
                  // destructor
                  Test::~Test()
                  {
                  }
                  
                  
                  void Test::resizeEvent(QResizeEvent* event)
                  {
                      if(!m_scene)
                          return;
                      QRectF bounds = m_scene->itemsBoundingRect();
                      m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                  }
                  

                  test.h

                  #ifndef TEST_H
                  #define TEST_H
                  
                  #include <QMainWindow>
                  #include <QGraphicsView>
                  #include <QGraphicsScene>
                  
                  class Test : public QMainWindow
                  {
                      Q_OBJECT
                  public:
                      explicit Test(QWidget* parent = nullptr);
                      ~Test() override;
                      void resizeEvent(QResizeEvent* event) override;
                  
                  public slots:
                  
                  private:
                      QGraphicsView *m_view;
                      QGraphicsScene *m_scene;
                  };
                  
                  #endif // TEST_H
                  

                  And here is what I get when I start the application:
                  Capture d’écran du 2022-06-22 10-27-39.png
                  So clearly not what I am expecting.

                  However, I get what I want when I resize the window:
                  Capture d’écran du 2022-06-22 10-27-49.png

                  The trick is done via the resizeEvent method. But I do not understand how can I do the same in the same in the constructor. Indeed

                  m_view->fitInView (m_scene->itemsBoundingRect(),Qt::IgnoreAspectRatio)
                  

                  does not do the job when it is placed at the end of the constructor but works fine in resizeEvent.

                  Any idea?

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

                  @odelaune
                  Sizes are not always set/right in constructors in Qt. For example, debug out what is returned from m_scene->itemsBoundingRect() for that line where you call it in the constructor, my guess is it's not the size it will end up being?

                  Size is only "correct" when the object has been shown, since that is when Qt can calculate/set it. So it would not be surprising if you have to redo this line in every resizeEvent....

                  O 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @odelaune
                    Sizes are not always set/right in constructors in Qt. For example, debug out what is returned from m_scene->itemsBoundingRect() for that line where you call it in the constructor, my guess is it's not the size it will end up being?

                    Size is only "correct" when the object has been shown, since that is when Qt can calculate/set it. So it would not be surprising if you have to redo this line in every resizeEvent....

                    O Offline
                    O Offline
                    odelaune
                    wrote on last edited by
                    #11

                    @JonB said in QGraphicsScene does not fit QGraphicsView full size:

                    For example, debug out what is returned from m_scene->itemsBoundingRect() for that line where you call it in the constructor, my guess is it's not the size it will end up being?

                    Actually, they are the same (QRectF(-5,-5 472x349)) both in the constructor and in the resizeEvent function. The value doesn't change either when I resize the window (still QRectF(-5,-5 472x349)). The same with sceneRect().

                    JonBJ 1 Reply Last reply
                    0
                    • O odelaune

                      @JonB said in QGraphicsScene does not fit QGraphicsView full size:

                      For example, debug out what is returned from m_scene->itemsBoundingRect() for that line where you call it in the constructor, my guess is it's not the size it will end up being?

                      Actually, they are the same (QRectF(-5,-5 472x349)) both in the constructor and in the resizeEvent function. The value doesn't change either when I resize the window (still QRectF(-5,-5 472x349)). The same with sceneRect().

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

                      @odelaune
                      But nonetheless you still say it does work when you do the fitInView() in the resizeEvent(), but not in the constructor, right??

                      Then I would basically read it as: fitInView() does not work "at all" in constructor before the view/scene is really shown, and that's just how it is. Assuming it's good in resizeEvent() you have your solution now anyway? (If it's still not right very first time when shown but no resizeEvent() is called, you can do it initially in showEvent() too?)

                      O 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @odelaune
                        But nonetheless you still say it does work when you do the fitInView() in the resizeEvent(), but not in the constructor, right??

                        Then I would basically read it as: fitInView() does not work "at all" in constructor before the view/scene is really shown, and that's just how it is. Assuming it's good in resizeEvent() you have your solution now anyway? (If it's still not right very first time when shown but no resizeEvent() is called, you can do it initially in showEvent() too?)

                        O Offline
                        O Offline
                        odelaune
                        wrote on last edited by
                        #13

                        @JonB said in QGraphicsScene does not fit QGraphicsView full size:

                        @odelaune
                        But nonetheless you still say it does work when you do the fitInView() in the resizeEvent(), but not in the constructor, right??

                        Yes, it does work in resizeEvent().

                        Then I would basically read it as: fitInView() does not work "at all" in constructor before the view/scene is really shown, and that's just how it is.

                        It makes sense.

                        Assuming it's good in resizeEvent() you have your solution now anyway? (If it's still not right very first time when shown but no resizeEvent() is called, you can do it initially in showEvent() too?)

                        What was missing was the implementation of showEvent(). This is now done and it fits well, even when not resizing.

                        Here is the working example code

                        test.cpp

                        // Qt headers
                        #include <QPixmap>
                        #include <QGraphicsPixmapItem>
                        #include <QGraphicsRectItem>
                        #include <QPainter>
                        #include <QPen>
                        #include <QDebug>
                        #include <QResizeEvent>
                        
                        // Other headers
                        #include "test.h"
                        
                        Test::Test(QWidget* parent /*=nullptr*/):
                            QMainWindow(parent)
                        {
                            m_view = new QGraphicsView(this);
                        
                            // draw border of the QGraphicsView
                            m_view->setStyleSheet("border: 5px solid red");
                        
                            m_scene = new QGraphicsScene(m_view);
                        
                            m_view->setRenderHint(QPainter::Antialiasing, true);
                            m_view->setScene(m_scene);
                            m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                            m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                        
                            QPixmap img("tmp.svg");
                            QGraphicsPixmapItem *item = m_scene->addPixmap(img);
                        
                            // draw border of the QGraphicsScene
                            QGraphicsRectItem *rect_item = m_scene->addRect(m_scene->sceneRect());
                            rect_item->setParentItem(item);
                            QPen pen = QPen(Qt::black);
                            pen.setWidth(10);
                            rect_item->setPen(pen);
                        
                            // set the mapView as the central widget
                            setCentralWidget(m_view);
                        
                            m_view->show();
                        }
                        
                        // destructor
                        Test::~Test()
                        {
                        }
                        
                        
                        void Test::resizeEvent(QResizeEvent* event)
                        {
                            if(!m_scene)
                                return;
                            QRectF bounds = m_scene->itemsBoundingRect();
                            m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                        }
                        
                        
                        void Test::showEvent(QShowEvent* event)
                        {
                            if(!m_scene)
                                return;
                            QRectF bounds = m_scene->itemsBoundingRect();
                            m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                        }
                        
                        

                        Thank you very much for your support.

                        JonBJ 1 Reply Last reply
                        0
                        • O odelaune

                          @JonB said in QGraphicsScene does not fit QGraphicsView full size:

                          @odelaune
                          But nonetheless you still say it does work when you do the fitInView() in the resizeEvent(), but not in the constructor, right??

                          Yes, it does work in resizeEvent().

                          Then I would basically read it as: fitInView() does not work "at all" in constructor before the view/scene is really shown, and that's just how it is.

                          It makes sense.

                          Assuming it's good in resizeEvent() you have your solution now anyway? (If it's still not right very first time when shown but no resizeEvent() is called, you can do it initially in showEvent() too?)

                          What was missing was the implementation of showEvent(). This is now done and it fits well, even when not resizing.

                          Here is the working example code

                          test.cpp

                          // Qt headers
                          #include <QPixmap>
                          #include <QGraphicsPixmapItem>
                          #include <QGraphicsRectItem>
                          #include <QPainter>
                          #include <QPen>
                          #include <QDebug>
                          #include <QResizeEvent>
                          
                          // Other headers
                          #include "test.h"
                          
                          Test::Test(QWidget* parent /*=nullptr*/):
                              QMainWindow(parent)
                          {
                              m_view = new QGraphicsView(this);
                          
                              // draw border of the QGraphicsView
                              m_view->setStyleSheet("border: 5px solid red");
                          
                              m_scene = new QGraphicsScene(m_view);
                          
                              m_view->setRenderHint(QPainter::Antialiasing, true);
                              m_view->setScene(m_scene);
                              m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                              m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
                          
                              QPixmap img("tmp.svg");
                              QGraphicsPixmapItem *item = m_scene->addPixmap(img);
                          
                              // draw border of the QGraphicsScene
                              QGraphicsRectItem *rect_item = m_scene->addRect(m_scene->sceneRect());
                              rect_item->setParentItem(item);
                              QPen pen = QPen(Qt::black);
                              pen.setWidth(10);
                              rect_item->setPen(pen);
                          
                              // set the mapView as the central widget
                              setCentralWidget(m_view);
                          
                              m_view->show();
                          }
                          
                          // destructor
                          Test::~Test()
                          {
                          }
                          
                          
                          void Test::resizeEvent(QResizeEvent* event)
                          {
                              if(!m_scene)
                                  return;
                              QRectF bounds = m_scene->itemsBoundingRect();
                              m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                          }
                          
                          
                          void Test::showEvent(QShowEvent* event)
                          {
                              if(!m_scene)
                                  return;
                              QRectF bounds = m_scene->itemsBoundingRect();
                              m_view->fitInView(bounds, Qt::IgnoreAspectRatio);
                          }
                          
                          

                          Thank you very much for your support.

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

                          @odelaune said in QGraphicsScene does not fit QGraphicsView full size:

                          What was missing was the implementation of showEvent(). This is now done and it fits well, even when not resizing.

                          Yes, For widgets it is showEvent() which is first called when it is shown. For anything "size" that is better than in constructor.

                          1 Reply Last reply
                          1
                          • Pl45m4P Pl45m4 referenced this topic on

                          • Login

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