Results 1 to 5 of 5

Thread: Fit In View a loaded image to QGraphicsView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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)

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

    thaihoangluu (22nd December 2011)

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.