Doubts about QGraphicsView fitInView method
-
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.
-
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.
@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.
-
@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.
@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?
-
@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?
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
offersscale()
to change the view's scale but not a method to return just the current scale. IIRC you have to calltransform()
to get the whole currentQTransform
and access the individual transformation members (m11()
andm22()
, 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.) -
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
offersscale()
to change the view's scale but not a method to return just the current scale. IIRC you have to calltransform()
to get the whole currentQTransform
and access the individual transformation members (m11()
andm22()
, 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.)@JonB said in Doubts about QGraphicsView fitInView method:
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
offersscale()
to change the view's scale but not a method to return just the current scale. IIRC you have to calltransform()
to get the whole currentQTransform
and access the individual transformation members (m11()
andm22()
, 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!