PDA

View Full Version : QGraphicsView: Images doesn't fit view frame



janck
10th June 2013, 19:06
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:


image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
scn = new QGraphicsScene(this); // object defined in header
ui->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?

wysota
10th June 2013, 20:56
How does the result you get differ from what you expect?

janck
10th June 2013, 21:46
Here is the picture of current state and what I want:
9137

wysota
11th June 2013, 00:34
What does scn->itemsBoundingRect() return?

karankumar1609
11th June 2013, 07:25
You have not set the sceneRect for QGraphicsScene....



image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
scn = new QGraphicsScene(this); // object defined 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);

janck
11th June 2013, 07:35
What does scn->itemsBoundingRect() return?

QRectF(0,0 1078x1922)


You have not set the sceneRect for QGraphicsScene....



image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
scn = new QGraphicsScene(this); // object defined 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.

wysota
11th June 2013, 07:54
QRectF(0,0 1078x1922)

And how large is your view's viewport?

janck
11th June 2013, 08:13
Output from ui->graphicsView->viewport->size() is QSize(98, 28) and the frame is about 200 x 400 px.

karankumar1609
11th June 2013, 09:28
You were trying to add an image in background of QGraphicsView

Try to add background image of QGraphicsView, or QGraphicsScene.

try:


ui->graphicsView->setBackgroundBrush(QBrush(*image));
// set scene background transparent. ;)


OR



ui->graphicsView->fitInView(scn->sceneRect(), Qt::KeepAspectRatio);


ALL I CAN SE IS THE PROBLEM OF SCENE RECT.

janck
11th June 2013, 10:11
None of that helped me, but I've changed fitInView's KeepAspectRatio to


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?

wysota
11th June 2013, 10:27
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.

janck
11th June 2013, 15:02
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?

wysota
11th June 2013, 15:28
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.

janck
12th June 2013, 10:51
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:


image = new QImage(filePath);
scn = new QGraphicsScene(this);
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!

wysota
12th June 2013, 11:44
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.

janck
12th June 2013, 17:42
Ok, I see what's the problem. But how can I do that image would fit the frame on setup of dialog?

wysota
13th June 2013, 09:23
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().

janck
13th June 2013, 13:07
Sorry, I didn't know that showEvent() also exists for Dialogs. This solved my problem, so thank you! :)