Results 1 to 5 of 5

Thread: Fit In View a loaded image to QGraphicsView

  1. #1
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Fit In View a loaded image to QGraphicsView

    anybody has ideas how to resize an image to fit the bounds of a qgraphicsview keeping aspect ratio... WITHOUT USING sceneRect() ? . ...

    how about an example for
    void QGraphicsScene::render ( QPainter * painter, const QRectF & target = QRectF(), const QRectF & source = QRectF(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio ) . . . .
    Last edited by sincnarf; 7th October 2007 at 06:01. Reason: typos
    Image Analysis Development Framework Using Qt (IADFUQ)

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

    Default Re: Fit In View a loaded image to QGraphicsView

    Why don't you want to use sceneRect()? You can resize the scene to the image and then fit the scene into the view, if that is an option for you.

  3. #3
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Fit In View a loaded image to QGraphicsView

    sincnarf, I don't understand... you can pass any rectangle to QGraphicsView::fitInView(). It's the first argument the function takes.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  4. The following user says thank you to Bitto for this useful post:

    sincnarf (11th October 2007)

  5. #4
    Join Date
    Apr 2007
    Posts
    117
    Thanks
    84
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fit In View a loaded image to QGraphicsView

    At last this code works.... Last question on this though... By default, pixmaps are added to the scene at the center. Am i correct? But In this code my x and y for the QRectF is always 0 and 0 respectively, so this means the added graphics item is not centered. How can I make the item centered on the view?

    Qt Code:
    1. void MainWindow::loadGraphicsViewParameter(QGraphicsView *graphicsView) {
    2.  
    3. QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"),
    4. QDir::currentPath());
    5. if (!fileName.isEmpty()) {
    6. QImage tempImage(fileName);
    7. if (tempImage.isNull()) {
    8. QMessageBox::information(this, tr("Load Warning"), tr("Cannot load %1.").arg(fileName));
    9. return;
    10. }
    11. QImage image = tempImage.convertToFormat(QImage::Format_RGB32);
    12. QPixmap pixmap = QPixmap::fromImage(image);
    13.  
    14. int width = graphicsView->geometry().width();
    15. int height = graphicsView->geometry().height();
    16. // QMessageBox::information(this, tr("WIDTH HEIGHT"), tr("Width= %1 Height= %2").arg(QString::number(width, 10), QString::number(height, 10)));
    17. QGraphicsScene *viewScene = new QGraphicsScene(QRectF(0, 0, width, height), 0);
    18. QGraphicsPixmapItem *item = viewScene->addPixmap(pixmap.scaled(QSize(
    19. (int)viewScene->width(), (int)viewScene->height()),
    20. Qt::KeepAspectRatio, Qt::SmoothTransformation));
    21. graphicsView->fitInView(QRectF(0, 0, width, height),
    22. Qt::KeepAspectRatio);
    23. graphicsView->setScene(viewScene);
    24. graphicsView->show();
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by sincnarf; 8th October 2007 at 11:16. Reason: wrong question
    Image Analysis Development Framework Using Qt (IADFUQ)

  6. The following user says thank you to sincnarf for this useful post:

    thaihoangluu (22nd December 2011)

  7. #5
    Join Date
    Sep 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Fit In View a loaded image to QGraphicsView

    Thanks for your post, this is really helpful for me ^^

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  3. Explanation to Image Formats
    By sincnarf in forum Qt Programming
    Replies: 13
    Last Post: 6th July 2007, 17:02
  4. no image displayed on QGraphicsView
    By sincnarf in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2007, 12:33

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
  •  
Qt is a trademark of The Qt Company.