What does scn->itemsBoundingRect() return?
You have not set the sceneRect for QGraphicsScene....
Qt 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); ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);To copy to clipboard, switch view to plain text mode
Output from ui->graphicsView->viewport->size() is QSize(98, 28) and the frame is about 200 x 400 px.
You were trying to add an image in background of QGraphicsView
Try to add background image of QGraphicsView, or QGraphicsScene.
try:
Qt Code:
// set scene background transparent. ;)To copy to clipboard, switch view to plain text mode
OR
Qt Code:
ui->graphicsView->fitInView(scn->sceneRect(), Qt::KeepAspectRatio);To copy to clipboard, switch view to plain text mode
ALL I CAN SE IS THE PROBLEM OF SCENE RECT.
None of that helped me, but I've changed fitInView's KeepAspectRatio to
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?Qt Code:
ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatioByExpanding);To copy to clipboard, switch view to plain text mode
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.
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?
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.
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:
Qt Code:
ui->graphicsView->setScene(scn); scn->setSceneRect(0,0,image->width(),image->height()); ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatioByExpanding);To copy to clipboard, switch view to plain text mode
What can I do to fit image into this frame?
Thank you very much!
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.
Ok, I see what's the problem. But how can I do that image would fit the frame on setup of dialog?
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)
Bookmarks