Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Resize image to fit in graphics view
Forum Updated to NodeBB v4.3 + New Features

Resize image to fit in graphics view

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5qtcreatorimageimageslistwidget
9 Posts 3 Posters 10.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kinesis
    wrote on 13 Jul 2018, 04:26 last edited by Kinesis
    #1

    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);
    
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Jul 2018, 22:14 last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 16 Jul 2018, 06:25
      1
      • S SGaist
        13 Jul 2018, 22:14

        Hi,

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

        K Offline
        K Offline
        Kinesis
        wrote on 16 Jul 2018, 06:25 last edited by
        #3

        @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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Jul 2018, 21:17 last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply 17 Jul 2018, 03:53
          0
          • S SGaist
            16 Jul 2018, 21:17

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

            K Offline
            K Offline
            Kinesis
            wrote on 17 Jul 2018, 03:53 last edited by
            #5

            @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)

            D 1 Reply Last reply 17 Jul 2018, 04:10
            0
            • K Kinesis
              17 Jul 2018, 03:53

              @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)

              D Offline
              D Offline
              Devopia53
              wrote on 17 Jul 2018, 04:10 last edited by
              #6

              @Kinesis

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

              K 1 Reply Last reply 17 Jul 2018, 04:38
              0
              • D Devopia53
                17 Jul 2018, 04:10

                @Kinesis

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

                K Offline
                K Offline
                Kinesis
                wrote on 17 Jul 2018, 04:38 last edited by
                #7

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

                D 1 Reply Last reply 17 Jul 2018, 04:44
                0
                • K Kinesis
                  17 Jul 2018, 04:38

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

                  D Offline
                  D Offline
                  Devopia53
                  wrote on 17 Jul 2018, 04:44 last edited by
                  #8

                  @Kinesis

                  Just use QLabel::setScaledContents(true).

                  K 1 Reply Last reply 17 Jul 2018, 04:52
                  2
                  • D Devopia53
                    17 Jul 2018, 04:44

                    @Kinesis

                    Just use QLabel::setScaledContents(true).

                    K Offline
                    K Offline
                    Kinesis
                    wrote on 17 Jul 2018, 04:52 last edited by
                    #9

                    @Devopia53
                    It works! , Thanks.

                    1 Reply Last reply
                    0

                    1/9

                    13 Jul 2018, 04:26

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved