Resize image to fit in graphics view



  • I have code that display image with its name on listWidget . In that case the image won't fit in the graphics view.I tested some of resizing methods.But it doesn't work.Please take a look my code.

    directory.setNameFilters({"*.png", "*.jpg"});
    
        for(const QFileInfo & finfo: directory.entryInfoList()){
            auto imageObject = new QImage();
            imageObject->load(finfo.absoluteFilePath());
    
            auto image = QPixmap::fromImage(*imageObject);
    
            QGraphicsScene *scene = new QGraphicsScene(this);
    
            scene->addPixmap(image);
            //scene->setSceneRect(image.rect());
            QGraphicsView *graphicsView = new QGraphicsView(scene);
            graphicsView->fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
    
      auto    item = new QListWidgetItem("", ui->listWidget_main);
            auto    widget = new QWidget;
            auto    label = new QLabel(finfo.fileName());
            auto    vb = new QVBoxLayout;
    
            vb->addWidget(label);
            vb->addWidget(graphicsView);
    
            widget->setLayout(vb);
            widget->setMinimumSize(340,340);
            ui->listWidget_main->setItemWidget(item,widget);
    
    }
    

  • Lifetime Qt Champion

    Hi,

    Why are you creating so many QGraphicsView objects and also why the QImage on the heap rather than on the stack ?



  • @SGaist
    Sorry for late reply, this is just one part of my main code. In my main code I need to display videos , images and textedits on listWidget . I used QIcon to display image thumbnails . But if I use QIcon , I can display all but the images are behind the other widgets. It seems like the images are not arranged in listWidget.Thats why I use graphics view to list in listWidget.Thanks


  • Lifetime Qt Champion

    Why are you using a QListWidget if it's only for showing more widgets ?



  • @SGaist
    "Why are you using a QListWidget if it's only for showing more widgets ?"
    For this question , so if I don't use QListWidget , what should I use?
    Suggestion please?
    Here is the output what is going on .
    0_1531799328876_a.png
    As U see , the images don't fit.(this is using Graphics View)



  • @Kinesis

    In that case, QLabel is enough. Using QGraphicsView is a very expensive cost.



  • @Devopia53
    OK , I can use QLabel , but how can I resize image to fit in label??
    Thanks for your suggestion.



  • @Kinesis

    Just use QLabel::setScaledContents(true).



  • @Devopia53
    It works! , Thanks.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.