Results 1 to 4 of 4

Thread: How to properly show image in QGraphicsView?

  1. #1
    Join Date
    Apr 2019
    Posts
    4
    Qt products
    Qt5

    Default How to properly show image in QGraphicsView?

    I'm trying to show an image in QGraphicsView, but for some reason the top-right corner starts in the middle of the QGraphicsView.
    Also, how to make it fit the QGraphicsView?

    Here's my code:

    Qt Code:
    1. Mat frameMat;
    2. videoReader >> frameMat;
    3.  
    4. _graphicsScene = make_unique<QGraphicsScene>(0, 0, 300, 300);
    5. ui.graphicsView->setScene(_graphicsScene .get());
    6. _graphicsScene ->setBackgroundBrush(Qt::blue);
    7.  
    8. QImage qImg(frameMat.data, frameMat.cols, frameMat.rows, frameMat.step, QImage::Format_RGB888);
    9. auto pixImg = QPixmap::fromImage(qImg);
    10.  
    11. auto item = new QGraphicsPixmapItem(pixImg);
    12. _graphicsScene ->addItem(item);
    To copy to clipboard, switch view to plain text mode 

    Here what I get when I run my code:
    Untitled.jpg

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to properly show image in QGraphicsView?

    The scene is displayed in the view at a 'natural' scale by default. If the view has more space available than the scene covers then you will see the background around the image.
    Your code does not show how the view is sized.

    You probably want to experiment with:
    Qt Code:
    1. ui.graphicsView->fitInView(_graphicsScene ->sceneRect(), Qt::KeepAspectRatio);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2019
    Posts
    4
    Qt products
    Qt5

    Default Re: How to properly show image in QGraphicsView?

    I tried to use
    Qt Code:
    1. _graphicsScene->setSceneRect(0, 0, 500, 500);
    To copy to clipboard, switch view to plain text mode 
    but it seems moves the image up and left. If I do
    Qt Code:
    1. _graphicsScene->setSceneRect(200, 200, 500, 500);
    To copy to clipboard, switch view to plain text mode 
    it moves the image top and left beyond the actual window. Why?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to properly show image in QGraphicsView?

    The scene coordinate system and the view coordinate system are not the same thing. There is a coordinate transformation between the two systems. That transformation maps coordinates in the scene onto view coordinates to make the scene visible in the view. It may be the entire scene, part of the scene, a rotated version etc. that ends up being visible. When the entire scene can fit in the view the QGraphicsView::alignment() comes into play: by default this centres. Assuming this is the case in your code then:
    Qt Code:
    1. view->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    To copy to clipboard, switch view to plain text mode 
    may help

Similar Threads

  1. Show background image to QGraphicsView
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 31st March 2017, 15:47
  2. QMenuBar doesn't show up properly
    By Cruz in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2014, 19:40
  3. Replies: 4
    Last Post: 15th February 2013, 11:55
  4. QGraphicsView::scale does not work properly.
    By metdos in forum Qt Programming
    Replies: 1
    Last Post: 18th January 2010, 09:58
  5. QGraphicsView not updating properly.
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 11:36

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.