Results 1 to 18 of 18

Thread: QGraphicsView: Images doesn't fit view frame

  1. #1
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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:
    Qt Code:
    1. image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
    2. scn = new QGraphicsScene(this); // object defined in header
    3. ui->graphicsView->setScene(scn);
    4. scn->addPixmap(QPixmap::fromImage(*image));
    5. ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Images doesn't fit view frame

    How does the result you get differ from what you expect?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsView: Images doesn't fit view frame

    Here is the picture of current state and what I want:
    USRlisR.png

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Images doesn't fit view frame

    What does scn->itemsBoundingRect() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QGraphicsView: Images doesn't fit view frame

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

    Qt Code:
    1. image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
    2. scn = new QGraphicsScene(this); // object defined in header
    3. // scn->setSceneRect(ui->graphicsView->Rect()); // SOMETHING LIHE THIS
    4. ui->graphicsView->setScene(scn);
    5. scn->addPixmap(QPixmap::fromImage(*image));
    6. ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsView: Images doesn't fit view frame

    Quote Originally Posted by wysota View Post
    What does scn->itemsBoundingRect() return?
    QRectF(0,0 1078x1922)

    Quote Originally Posted by karankumar1609 View Post
    You have not set the sceneRect for QGraphicsScene....

    Qt Code:
    1. image = new QImage(data.absoluteFilePath()); // variable data is defined when calling this method and object image is defined as QImage in header
    2. scn = new QGraphicsScene(this); // object defined in header
    3. // scn->setSceneRect(ui->graphicsView->Rect()); // SOMETHING LIHE THIS
    4. ui->graphicsView->setScene(scn);
    5. scn->addPixmap(QPixmap::fromImage(*image));
    6. ui->graphicsView->fitInView(scn->itemsBoundingRect(),Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 
    It's same as before.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Images doesn't fit view frame

    Quote Originally Posted by janck View Post
    QRectF(0,0 1078x1922)
    And how large is your view's viewport?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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.

  9. #9
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. ui->graphicsView->setBackgroundBrush(QBrush(*image));
    2. // set scene background transparent. ;)
    To copy to clipboard, switch view to plain text mode 

    OR

    Qt Code:
    1. 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.

  10. #10
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsView: Images doesn't fit view frame

    None of that helped me, but I've changed fitInView's KeepAspectRatio to
    Qt Code:
    1. ui->graphicsView->fitInView(scn->sceneRect(),Qt::KeepAspectRatioByExpanding);
    To copy to clipboard, switch view to plain text mode 
    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?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView: Images doesn't fit view frame

    Quote Originally Posted by janck View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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:
    Qt Code:
    1. image = new QImage(filePath);
    2. scn = new QGraphicsScene(this);
    3. ui->graphicsView->setScene(scn);
    4. scn->addPixmap(QPixmap::fromImage(*image));
    5. scn->setSceneRect(0,0,image->width(),image->height());
    6. 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!

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #16
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. The following user says thank you to wysota for this useful post:

    janck (13th June 2013)

  19. #18
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    1
    Qt products
    Qt5

    Default 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!

Similar Threads

  1. QPixMap doesn't show one of two images !
    By ladiesfinger in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2011, 16:29
  2. QGraphicsView Frame Rate
    By benlau in forum Qt Programming
    Replies: 11
    Last Post: 13th October 2010, 01:04
  3. How to drag and drop on QGraphicsView frame
    By wudelei in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2010, 08:04
  4. Application doesn't display images
    By satoshi in forum Qt Programming
    Replies: 3
    Last Post: 2nd January 2010, 11:33
  5. Simple frame control doesn't resize when I hide()
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 29th August 2007, 22:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.