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. Doubts about QGraphicsView fitInView method
Forum Updated to NodeBB v4.3 + New Features

Doubts about QGraphicsView fitInView method

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 556 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.
  • Z Offline
    Z Offline
    ZeusFa
    wrote on last edited by ZeusFa
    #1

    Doubts about QGraphicsView fitInView method

    I customized QGraphicsView and overloaded resizeEvent.

    The effect I want to achieve is:

    When the window size of the window changes, QGraphicsView changes accordingly.
    So I wrote the following code:

    CustomGraphicsView.h

    #include <QGraphicsScene>
    #include <QGraphicsItem>
    #include <QGraphicsView>
    
    class CustomGraphicsView: public QGraphicsView {
        // variables:
         Q_OBJECT
    
    public:
        // constructor.
        CustomGraphicsView();
        // destructor.
        virtual ~CustomGraphicsView() = default;
        // disabled:
        CustomGraphicsView(const CustomGraphicsView&) = delete;
        CustomGraphicsView& operator=(const CustomGraphicsView&) = delete;
    
    
    protected:
        void paintEvent(QPaintEvent *pQEvent) override;
        void resizeEvent(QResizeEvent *event) override;
    };
    #endif
    

    CustomGraphicsView.cpp

    CustomGraphicsView::CustomGraphicsView() : QGraphicsView()
    {
    	setMaximumSize(620, 400);
    	setMinimumSize(457, 246);
    	setFrameStyle(QFrame::NoFrame);
    
    	setStyleSheet(
    		"background-color: transparent; QGraphicsView { border-style: none; }");
    
    }
    
    void STTCanvasView::paintEvent(QPaintEvent *pQEvent)
    {
    	QGraphicsView::paintEvent(pQEvent);
    }
    
    void STTCanvasView::resizeEvent(QResizeEvent *event)
    {
    	QSize old_size = event->oldSize();
    	QSize new_size = event->size();
    
    	QGraphicsScene *pQGScene = scene();
    	
    	fitInView(pQGScene->sceneRect(), Qt::KeepAspectRatio);
    
    	QList<QGraphicsItem *> listAll = pQGScene->items();
    
    	for (int i = 0; i < listAll.count(); i++) {
    		QGraphicsItem *it = listAll.at(i);
    		qDebug() << "item->boundingRect()"
    			 << it->boundingRect();
    	}
    	QGraphicsView::resizeEvent(event);
    }
    

    When the size of the windows window changes, I print out the boundingRect of the scene, but I find that no matter how I adjust the size of the window, the boudingRect remains unchanged.

    for (int i = 0; i < listAll.count(); i++) {
    		QGraphicsItem *it = listAll.at(i);
    		qDebug() << "item->boundingRect()"
    			 << it->boundingRect();
    	}
    

    My intention is that when the window size changes, since I use the fitInView method, the size of the item in the QGraphicsScene will also change accordingly.

    However, because the item is sized through the boundingRect method, for example:

    MyGraphicsItem.h

    class MyGraphicsItem: public QObject, public QGraphicsItem {
    	Q_OBJECT
    	Q_INTERFACES(QGraphicsItem)
    public:
    	MyGraphicsItem();
    
    	QRectF boundingRect() const override;
    	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    		   QWidget *widget) override;
    
    private:
            QRectF rSize;
    }
    

    MyGraphicsItem.cpp

    MyGraphicsItem::MyGraphicsItem(const QString &posStr)
    	: QObject(),rSize(0,0,100,100)
    {
    }
    
    QRectF MyGraphicsItem::boundingRect() const
    {
    	return rSize;
    }
    
    void MyGraphicsItem::paint(QPainter *painter,
    			  const QStyleOptionGraphicsItem *option,
    			  QWidget *widget)
    {
              //.....
    }
    

    My intention is that when the window size changes, since I use the fitInView method, the size of the item in the QGraphicsScene will also change accordingly.

    However, because the item is sized through the boundingRect method,

    So no matter how the size of the windows changes, the boundingRect of the item will not change, but in fact, due to the use of fitInView, the appearance of the item seems to have changed with the size, but what method should I use to obtain the changed item boundingRect?

    I am referring to the real item boundingRect size, for example, the windows window changes from 600,400 to 300,200. In appearance, the item does change from the original 100,100 to 50,50, but the rect I obtained through item->boundingRect() is still 100,100.

    JonBJ 1 Reply Last reply
    0
    • Z ZeusFa

      Doubts about QGraphicsView fitInView method

      I customized QGraphicsView and overloaded resizeEvent.

      The effect I want to achieve is:

      When the window size of the window changes, QGraphicsView changes accordingly.
      So I wrote the following code:

      CustomGraphicsView.h

      #include <QGraphicsScene>
      #include <QGraphicsItem>
      #include <QGraphicsView>
      
      class CustomGraphicsView: public QGraphicsView {
          // variables:
           Q_OBJECT
      
      public:
          // constructor.
          CustomGraphicsView();
          // destructor.
          virtual ~CustomGraphicsView() = default;
          // disabled:
          CustomGraphicsView(const CustomGraphicsView&) = delete;
          CustomGraphicsView& operator=(const CustomGraphicsView&) = delete;
      
      
      protected:
          void paintEvent(QPaintEvent *pQEvent) override;
          void resizeEvent(QResizeEvent *event) override;
      };
      #endif
      

      CustomGraphicsView.cpp

      CustomGraphicsView::CustomGraphicsView() : QGraphicsView()
      {
      	setMaximumSize(620, 400);
      	setMinimumSize(457, 246);
      	setFrameStyle(QFrame::NoFrame);
      
      	setStyleSheet(
      		"background-color: transparent; QGraphicsView { border-style: none; }");
      
      }
      
      void STTCanvasView::paintEvent(QPaintEvent *pQEvent)
      {
      	QGraphicsView::paintEvent(pQEvent);
      }
      
      void STTCanvasView::resizeEvent(QResizeEvent *event)
      {
      	QSize old_size = event->oldSize();
      	QSize new_size = event->size();
      
      	QGraphicsScene *pQGScene = scene();
      	
      	fitInView(pQGScene->sceneRect(), Qt::KeepAspectRatio);
      
      	QList<QGraphicsItem *> listAll = pQGScene->items();
      
      	for (int i = 0; i < listAll.count(); i++) {
      		QGraphicsItem *it = listAll.at(i);
      		qDebug() << "item->boundingRect()"
      			 << it->boundingRect();
      	}
      	QGraphicsView::resizeEvent(event);
      }
      

      When the size of the windows window changes, I print out the boundingRect of the scene, but I find that no matter how I adjust the size of the window, the boudingRect remains unchanged.

      for (int i = 0; i < listAll.count(); i++) {
      		QGraphicsItem *it = listAll.at(i);
      		qDebug() << "item->boundingRect()"
      			 << it->boundingRect();
      	}
      

      My intention is that when the window size changes, since I use the fitInView method, the size of the item in the QGraphicsScene will also change accordingly.

      However, because the item is sized through the boundingRect method, for example:

      MyGraphicsItem.h

      class MyGraphicsItem: public QObject, public QGraphicsItem {
      	Q_OBJECT
      	Q_INTERFACES(QGraphicsItem)
      public:
      	MyGraphicsItem();
      
      	QRectF boundingRect() const override;
      	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
      		   QWidget *widget) override;
      
      private:
              QRectF rSize;
      }
      

      MyGraphicsItem.cpp

      MyGraphicsItem::MyGraphicsItem(const QString &posStr)
      	: QObject(),rSize(0,0,100,100)
      {
      }
      
      QRectF MyGraphicsItem::boundingRect() const
      {
      	return rSize;
      }
      
      void MyGraphicsItem::paint(QPainter *painter,
      			  const QStyleOptionGraphicsItem *option,
      			  QWidget *widget)
      {
                //.....
      }
      

      My intention is that when the window size changes, since I use the fitInView method, the size of the item in the QGraphicsScene will also change accordingly.

      However, because the item is sized through the boundingRect method,

      So no matter how the size of the windows changes, the boundingRect of the item will not change, but in fact, due to the use of fitInView, the appearance of the item seems to have changed with the size, but what method should I use to obtain the changed item boundingRect?

      I am referring to the real item boundingRect size, for example, the windows window changes from 600,400 to 300,200. In appearance, the item does change from the original 100,100 to 50,50, but the rect I obtained through item->boundingRect() is still 100,100.

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

      @ZeusFa
      Changing a view does not alter what is on the scene or alter the scene in any way.

      QGraphicsView::fitInView() just alters the scale of the view. (QGraphicsView::scale()):

      Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.

      Z 1 Reply Last reply
      0
      • JonBJ JonB

        @ZeusFa
        Changing a view does not alter what is on the scene or alter the scene in any way.

        QGraphicsView::fitInView() just alters the scale of the view. (QGraphicsView::scale()):

        Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.

        Z Offline
        Z Offline
        ZeusFa
        wrote on last edited by
        #3

        @JonB said in Doubts about QGraphicsView fitInView method:

        @ZeusFa
        Changing a view does not alter what is on the scene or alter the scene in any way.

        QGraphicsView::fitInView() just alters the scale of the view. (QGraphicsView::scale()):

        Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.

        So fitInView doesn't change the size of the scene? So why does it seem that the item changes with the window size?

        How can I get the scale value after fitInView?

        JonBJ 1 Reply Last reply
        0
        • Z ZeusFa

          @JonB said in Doubts about QGraphicsView fitInView method:

          @ZeusFa
          Changing a view does not alter what is on the scene or alter the scene in any way.

          QGraphicsView::fitInView() just alters the scale of the view. (QGraphicsView::scale()):

          Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport.

          So fitInView doesn't change the size of the scene? So why does it seem that the item changes with the window size?

          How can I get the scale value after fitInView?

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

          @ZeusFa

          So fitInView doesn't change the size of the scene?

          I certainly hope not! It's a view onto the scene, it should not change anything on the scene. If you take a photo of a cat, and the cat is too small to see, to make it fill the view you don't change the size of the cat, you zoom the view instead :)

          How can I get the scale value after fitInView?

          Irritatingly QGraphicsView offers scale() to change the view's scale but not a method to return just the current scale. IIRC you have to call transform() to get the whole current QTransform and access the individual transformation members (m11() and m22(), I think) if you want retrieve the current scale.

          Try printing these out before and after fitInView() and I think you will see those change. (You may also see the origin/translation change if fitting in view alters the view's top-left corner position on the scene. The others should not change.)

          Z 1 Reply Last reply
          1
          • JonBJ JonB

            @ZeusFa

            So fitInView doesn't change the size of the scene?

            I certainly hope not! It's a view onto the scene, it should not change anything on the scene. If you take a photo of a cat, and the cat is too small to see, to make it fill the view you don't change the size of the cat, you zoom the view instead :)

            How can I get the scale value after fitInView?

            Irritatingly QGraphicsView offers scale() to change the view's scale but not a method to return just the current scale. IIRC you have to call transform() to get the whole current QTransform and access the individual transformation members (m11() and m22(), I think) if you want retrieve the current scale.

            Try printing these out before and after fitInView() and I think you will see those change. (You may also see the origin/translation change if fitting in view alters the view's top-left corner position on the scene. The others should not change.)

            Z Offline
            Z Offline
            ZeusFa
            wrote on last edited by
            #5

            @JonB said in Doubts about QGraphicsView fitInView method:

            @ZeusFa

            So fitInView doesn't change the size of the scene?

            I certainly hope not! It's a view onto the scene, it should not change anything on the scene. If you take a photo of a cat, and the cat is too small to see, to make it fill the view you don't change the size of the cat, you zoom the view instead :)

            How can I get the scale value after fitInView?

            Irritatingly QGraphicsView offers scale() to change the view's scale but not a method to return just the current scale. IIRC you have to call transform() to get the whole current QTransform and access the individual transformation members (m11() and m22(), I think) if you want retrieve the current scale.

            Try printing these out before and after fitInView() and I think you will see those change. (You may also see the origin/translation change if fitting in view alters the view's top-left corner position on the scene. The others should not change.)

            Got it, thank you!

            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