QGraphicsView: Images doesn't fit view frame
I'm working on program which shows user some picture that is selected by him. But there is a problem because I would like to fit this picture in QGraphicsView's frame and the picture is really smaller than the frame.
So here's my code:
Code:
image
= new QImage(data.
absoluteFilePath());
// variable data is defined when calling this method and object image is defined as QImage in headerui->graphicsView->setScene(scn);
scn
->addPixmap
(QPixmap::fromImage(*image
));
ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
I was trying a lot of solutions that I found on web, but no one didn't help me. The picture is in size around 40 x 60 px when the frame is 200 x 400 px. What could be wrong?
Re: QGraphicsView: Images doesn't fit view frame
How does the result you get differ from what you expect?
1 Attachment(s)
Re: QGraphicsView: Images doesn't fit view frame
Here is the picture of current state and what I want:
Attachment 9137
Re: QGraphicsView: Images doesn't fit view frame
What does scn->itemsBoundingRect() return?
Re: QGraphicsView: Images doesn't fit view frame
You have not set the sceneRect for QGraphicsScene....
Code:
image
= new QImage(data.
absoluteFilePath());
// variable data is defined when calling this method and object image is defined as QImage in header // scn->setSceneRect(ui->graphicsView->Rect()); // SOMETHING LIHE THIS
ui->graphicsView->setScene(scn);
scn
->addPixmap
(QPixmap::fromImage(*image
));
ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
Re: QGraphicsView: Images doesn't fit view frame
Quote:
Originally Posted by
wysota
What does scn->itemsBoundingRect() return?
QRectF(0,0 1078x1922)
Quote:
Originally Posted by
karankumar1609
You have not set the
sceneRect for
QGraphicsScene....
Code:
image
= new QImage(data.
absoluteFilePath());
// variable data is defined when calling this method and object image is defined as QImage in header // scn->setSceneRect(ui->graphicsView->Rect()); // SOMETHING LIHE THIS
ui->graphicsView->setScene(scn);
scn
->addPixmap
(QPixmap::fromImage(*image
));
ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
It's same as before.
Re: QGraphicsView: Images doesn't fit view frame
Quote:
Originally Posted by
janck
QRectF(0,0 1078x1922)
And how large is your view's viewport?
Re: QGraphicsView: Images doesn't fit view frame
Output from ui->graphicsView->viewport->size() is QSize(98, 28) and the frame is about 200 x 400 px.
Re: QGraphicsView: Images doesn't fit view frame
You were trying to add an image in background of QGraphicsView
Try to add background image of QGraphicsView, or QGraphicsScene.
try:
Code:
ui
->graphicsView
->setBackgroundBrush
(QBrush(*image
));
// set scene background transparent. ;)
OR
Code:
ui->graphicsView->fitInView(scn->sceneRect(), Qt::KeepAspectRatio);
ALL I CAN SE IS THE PROBLEM OF SCENE RECT.
Re: QGraphicsView: Images doesn't fit view frame
None of that helped me, but I've changed fitInView's KeepAspectRatio to
Code:
ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatioByExpanding);
Image is now little bigger than before, but that is not solution. Is there any other way to insert image into dialog's form, so I can apply images with all aspect ratios and there wouldn't be visible border?
Re: QGraphicsView: Images doesn't fit view frame
Quote:
Originally Posted by
janck
Output from ui->graphicsView->viewport->size() is QSize(98, 28) and the frame is about 200 x 400 px.
If you are trying to call fitInView() before the view is first shown then this will not work. The view only gets a real size when it is first shown. You can either reimplement showEvent() or use a singleshot timer or QMetaObject::invokeMethod() to delay calling fitInView.
Re: QGraphicsView: Images doesn't fit view frame
It seems that there is no solution for QGraphicsView. I'm looking at QLabel which also provides support for QPixmap and looks it's easier to show image in here. What would be the command for fitting image into QLabel, because scaledContents only scale image, so it doesn't keep ratio?
Re: QGraphicsView: Images doesn't fit view frame
There is no equivalent. You can implement a custom widget that does what you want or use QGraphicsView. I assure you the latter works, I've used in many times like that. However implementing a custom widget is very easy.
Re: QGraphicsView: Images doesn't fit view frame
I found another code on web and adjusted it to my project, but it's same as before. I really don't know what could be wrong and as well I don't understand you last tips - probably because I'm into Qt only few weeks. Here is my current code:
Code:
ui->graphicsView->setScene(scn);
scn
->addPixmap
(QPixmap::fromImage(*image
));
scn->setSceneRect(0,0,image->width(),image->height());
ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatioByExpanding);
What can I do to fit image into this frame?
Thank you very much!
Re: QGraphicsView: Images doesn't fit view frame
I already told you -- you cannot call fitInView() before the view is first shown because it has no size yet. You can find as many "codes on web" but they will all fail because of the same reason. You need to do as I told you -- call fitInView() when the view is already visible.
Re: QGraphicsView: Images doesn't fit view frame
Ok, I see what's the problem. But how can I do that image would fit the frame on setup of dialog?
Re: QGraphicsView: Images doesn't fit view frame
Am I wasting my time here? Did you read post #11? You have two possible solutions there. And in #13 there is a suggestion to implement a custom widget that draws a resized pixmap. It's as easy as subclassing QWidget, reimplementing its paintEvent() and calling QPainter::drawPixmap().
Re: QGraphicsView: Images doesn't fit view frame
Sorry, I didn't know that showEvent() also exists for Dialogs. This solved my problem, so thank you! :)