Scaling the QGraphicsScene to fill whole QGraphicsView - c++

I have found a few fixes for this issue, but none of seemed to be sufficient. I have a QGraphicsView displaying a QGraphicsScene. What I want to do is to scale the scene in order to fill the whole view. Also I want it to scale dynamically when the user will be resizing the window displaying the view. Is such a thing possible? If so I'd be glad if you could give me a short example on how should it be implemented. Thanks in advance.

You can make your custom class which inherits from QGraphicsView. You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:
void MyView::resizeEvent(QResizeEvent *event)
{
fitInView(0, 0, 500, 500,Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.

Related

Qt: issue with QGraphicscene to show an imge

I am trying to develop very simple image viewer using QT. I am using the following code to show the image on QGraphicsScene widget:
QImage image(fileName);
firstPicture = new QGraphicsScene();
firstPicture->addPixmap(QPixmap::fromImage(image));
ui->graphicsView->setScene(firstPicture);
ui->graphicsView->fitInView(firstPicture->sceneRect() ,Qt::KeepAspectRatio);
I am getting the following output:
How can I fit the image into the GraphicsScene?
This approach works only when view showed your scene. In your case you did not call show(). Solution:
Use your approach when scene already shown.
You can reimplement showEvent and use your approach here.
You can scale image by yourself and set this scaled image to scene.
Also you can try to use: Qt::IgnoreAspectRatio instead of Qt::KeepAspectRatio.
You can make your custom class which inherits from QGraphicsView. You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:
void MyView::resizeEvent(QResizeEvent *event)
{
fitInView(firstPicture->sceneRect(), Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.

How to keep the same scene portion in view during resize?

I am using QGraphicsView to show something like function graphs. QGraphicsView keeps its transformation during resize, so the visible portion of the scene changes.
I need to keep the view exactly the same during resize - aspect ratio don't have to be kept since I allow independent scaling on X and Y axes.
This does not work:
void resizeEvent(QResizeEvent *event) override
{
QRectF rect = mapToScene(this->viewport()->rect()).boundingRect();
QGraphicsView::resizeEvent(event);
fitInView(rect);
}
Problem is in getting the proper view rectangle. This solution zooms out on resize.
Is there some simple way to achieve this?
I had the exact same problem. You will need to scale the view manually when its size changes, and restore the old center point. In python:
def resizeEvent(self, event):
w_ratio = self.rect().width() / event.oldSize().width()
h_ratio = self.rect().height() / event.oldSize().height()
self.scale(w_ratio, h_ratio)
self.centerOn(self.old_center)
You obviously need to set self.old_center earlier, e.g. in showEvent(). If the view can be translated by the user you will also need to update self.old_center in mouseReleaseEvent().
def showEvent(self, event):
self.old_center = self.mapToScene(self.rect().center())
def mouseReleaseEvent(self, event):
super(MyView, self).mouseReleaseEvent(event)
self.old_center = self.mapToScene(self.rect().center())
After making your custom class which inherits from QGraphicsView, you should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:
void MyView::resizeEvent(QResizeEvent *event)
{
QRectF rect = this->scene()->itemsBoundingRect();
fitInView(rect);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.
What about keeping the scene rectangle visualized by this view in the view with fitInView?
void resizeEvent(QResizeEvent *event) override
{
QGraphicsView::resizeEvent(event);
fitInView(this->sceneRect());
}
I realize this is quite old, but I came across the same challenge and found a solution that worked well.
I think the issue is that within the resize event, the viewport coords (mapped to the scene) are being fetched. In this case, the viewport has already changed (and thus the displayed scene), then fitInView is called with some slightly updated scene coordinates and so the display scene actually changes. This turns into a cascade like effect.
What we want to do is always pass the same scene rect to fitInView during the resize event. I was able to accomplish this by locally storing the scene coords when I first load the image and then anytime the displayed scene changes (pan/zoom/reset view, etc). Then when the resize event is called, we're always passing the same scene coordinates and don't getting caught in that change loop.

Scrolling in QGraphicsView

I have problem with scrolling in QGraphicsView.
I've set scene rect in my QGraphicsView subclass:
MyQGraphicsView::MyQGraphicsView{
setSceneRect(0,0,2000,2000)
}
And overloaded paintEvent:
void MyQGraphicsView::paintEvent(QPaintEvent *event){
qDebug()<<"Paint event";
QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing);
paint(painter);
}
void MyQGraphicsView::paint(QPainter &painter){
painter.setPen(Qt::NoPen);
painter.fillRect(QRect(0,0,200,200),Qt::gray);
painter.fillRect(QRect(500,500,1000,100),Qt::green);
painter.setPen(QPen(Qt::white,4,Qt::DashLine));
painter.drawLine(QLine(0,35,200,35));
painter.drawLine(QLine(0,165,200,165));
}
When I scroll the second rectangle is not visible. When I resize window it is. Also when scrolling rectangle is extending in wired way.
How should scrolling be implemened in this case? I've found several topics about scrolling in QGraphicsView but none solves my problem.
QGraphicsView inherits QAbstractScrollArea. So its content is displayed in its internal widget that can be obtained using viewport(). If you want to paint something and be able to scroll it, you need to attach an event filter to viewport widget and process its paintEvent, not view's event.
But you should not do this for QGraphicsView. You're trying to do something terribly wrong. You should not reimplement QGraphicsView::paintEvent just to paint something! It totally devalues its advantages. You need to use QGraphicsScene to add something to the view.

Viewing entire QGraphicsScene

I'm trying to write a map editor in Qt, using QGraphicsView and QGraphicsScene for both the map and tile sheets.
The problem I'm having right now is with making a good widget for importing tiles. For this, I'm using a QTabWidget (for different tile sheets), and TileWidget as the widget for each tab, which contains the QGraphicsScene and QGraphicsView.
It's working to a rough degree, but not all the tiles (or TileObjects, which are implementations of QGraphicsItem) are visible. I'm even calling view->ensureVisible(scene->sceneRect()), but still not all of the QGraphicsScene is not visible, even with scroll bars.
I understand this is due to limiting the maximum size of my QTabWidget, but that is necessary.
This happens mainly when I import a larger tile sheet.
I have a TileWidget as the QWidget for the QTabWidget, which has both the QGraphicsScene and the QGraphicsView.
TileWidget::TileWidget(QWidget *parent)
: QWidget(parent)
{
scene = new QGraphicsScene;
view = new TileView(scene, this);
connect(view, SIGNAL(newBrushSelected(TileObject *b)), this, SLOT(selectNewBrush(TileObject *b)));
}
TileView is simply a QGraphicsView re-implemented to handle mouse release events.
To add tiles, I simply call scene->addItem().
I have no other code for TileView. When I use
void TileWidget::showEvent(QShowEvent *event)
{
view->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
I get something like this.
It's okay for smaller tile sheets, but not for larger ones. What should I add to keep the size of the tiles normal, and navigate TileView using scroll bars?
Nevermind, figured it out. Just me being stupid.
You need something like:
p_myGraphicsView->fitInView(
myGraphicsView->scene()->itemsBoundingRect(),
Qt::KeepAspectRatio);

Qt - QGraphicsView without ScrollBar

I am trying to show a picture in it's full view using QGraphicsScene. But when ever I put the QgraphicsScene inside the QGraphicsView, I am getting a scroll bar. I tried so many ways But all are went to veins. So can anybody tell me how to obtain the full view without the scrollbar.
You might be getting scrollbars because the scene is larger than the usable area within the graphics view. By default, a QGraphicsView comes with a 1-pixel margin. To fix this, you can try:
QRect rcontent = graphicsView.contentsRect();
graphicsView.setSceneRect(0, 0, rcontent.width(), rcontent.height());
I had been getting scrollbars because I was manually setting the scene rect to the size of the graphics item I was adding -- which was as large as the QGraphicsView widget. I wasn't taking into account the margin.
QGraphicsView v;
v.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
v.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
To adjust the scrolling programmatically once these have been hidden, use one of the overloads of v.ensureVisible().