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. [solved]Setting Images in QListWidget

[solved]Setting Images in QListWidget

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 7.3k Views
  • 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.
  • V Offline
    V Offline
    venkatesh
    wrote on last edited by
    #1

    Hi,

    I have created a class for QListWidget to set data onto it and promoted the QListWidget from ui to the class. But i am not able to display the images in the listWidget (promoted class). I am confused what am i doing wrong

    listWidget.cpp

    @

    listWidget::listWidget(QWidget *parent)
    : QListWidget(parent)
    {

    setViewMode(QListWidget::IconMode);
    setStyleSheet(" border: 2px solid rgb(130,170,245)");
    setStyleSheet("background-color:rgb(235,240,255)");
    

    }
    void listWidget::DisplayImages()
    {
    QString ImageFileName;
    QStringList ImageFileNames;
    QFileDialog ImportImagesDialog;
    ImportImagesDialog.setFileMode(QFileDialog::ExistingFiles);
    if (ImportImagesDialog.exec())
    ImageFileNames = ImportImagesDialog.selectedFiles();
    foreach (ImageFileName, ImageFileNames)
    {
    QListWidgetItem *Image = new QListWidgetItem();
    QVariant ImageVariant(ImageFileName);
    Image->setData(Qt::UserRole,ImageVariant);
    Image->setText(ImageFileName);
    QIcon icon;
    setIconSize(QSize(100,100));
    icon.addPixmap(ImageFileName);
    Image->setIcon(icon);
    update();

         }
    

    }

    @

    @

    #ifndef LISTWIDGET_H
    #define LISTWIDGET_H

    #include<QListWidget>

    class QWidget;

    class listWidget : public QListWidget
    {
    Q_OBJECT
    public:
    explicit listWidget(QWidget *parent = 0);

    //QListWidgetItem *StationaryImage_=new QListWidgetItem();
    

    // QListWidgetItem *MovingImage_=new QListWidgetItem();
    public slots:
    void DisplayImages();
    // void StationaryImageItem();
    // void MovingImageItem();

    };
    #endif // LISTWIDGET_H

    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From a quick overview, you never set your items in your QListWidget

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

      1 Reply Last reply
      0
      • V Offline
        V Offline
        venkatesh
        wrote on last edited by
        #3

        Hi,

        Thanks for your response, i did add it after posting here
        But, i am confused how to do it.

        When i created a pointer to the class, or create an object nothing happens. How can i set the items in the listWidget ?

        I tried

        @

        listWidget *list;
        list->addItem(Image);

        @

        also just

        @

        addItem(Image);

        @

        Both are not working, how else can i set the items?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you check that you set a valid pixmap on your icon ?

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

          1 Reply Last reply
          0
          • V Offline
            V Offline
            venkatesh
            wrote on last edited by
            #5

            Hi,

            When i directly add the code in the listWidget class its working, it just executes immediately when i run the ui without main window being displayed. But when i try using the signal, slot in another class as such

            @

            connect(ui->ImportImages,SIGNAL(clicked()),&listObject_, SLOT(setData_()));
            

            @

            The file dialog opens but the images are not displayed in listWidget. I don't know whats the problem

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you post your constructor code (the one containing this connect statement) ?

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

              1 Reply Last reply
              0
              • V Offline
                V Offline
                venkatesh
                wrote on last edited by
                #7

                @ connect(ui->ImportImages,SIGNAL(clicked()),&listObject_, SLOT(setData_())); @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Not just the same connect statement, the complete constructor code

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

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    venkatesh
                    wrote on last edited by
                    #9

                    @

                    ImageRegistration::ImageRegistration(QWidget *parent) :
                    QWidget(parent),
                    ui(new Ui::ImageRegistration)
                    {

                    ui->setupUi(this);
                    ui->ExportTransformationMatrix->setStyleSheet("border-style:inset");
                    ui->ExportTransformationMatrix->setStyleSheet("border-width: 2px");
                    
                    
                    ui->ManualRegistrationView->setStyleSheet(" border: 2px solid rgb(130,170,245)");
                    ui->StationaryImageView->setStyleSheet(" border: 2px solid rgb(130,170,245)");
                    ui->MovingImageView->setStyleSheet("border: 2px solid rgb(130,170,245)");
                    ui->ZoomImage->setStyleSheet("border: 2px solid black ");
                    
                    connect(ui->ZoomImage,SIGNAL(valueChanged(int)),this,SLOT(updateTransform()));
                    connect(ui->RotateImage,SIGNAL(valueChanged(int)),this,SLOT(updateTransform()));
                    connect(ui->ImportImages,SIGNAL(clicked()),&listObject_, SLOT(setData_()));
                    

                    }

                    @

                    I have initialised the object in header as @ listWidget listObject_; @

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      You don't show listObject_.

                      And since it's not in any layout, it won't be automatically placed/resized for you

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

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        venkatesh
                        wrote on last edited by
                        #11

                        Hi,

                        Thanks for the response. When i added
                        @listObject_.show(); @ its shown in separate window, how can i make it visible in listWidget? I thought when we create an object, and apply the slot it would be placed in the class from which object is created. If we have to specify it manually is there a way to do it?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          It won't automagically be layouted because you create it in a widget.

                          You need to either add it to your ui file (using designer) or add it to your widgets layout.

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

                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            venkatesh
                            wrote on last edited by
                            #13

                            Hi,
                            I have already promoted listWidget class to the designer. How can i promote its object ? I tried the following but no use.How else can i add it to the ui

                            @

                            ui->ImageList=&listObject;
                            ui->ImageList->show();

                            @

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              venkatesh
                              wrote on last edited by
                              #14

                              Hi,

                              Solved this problem using the following in

                              ImageRegistration constructor

                              @
                              listWidget *listObject=ui->ImageList;
                              @

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                This is not a clean solution.
                                Remove listWidget from your code and only call ui->ImageList. You are starting evil things there (have a look about at the QObject documentation to know why copying QObject/QObject derived class is not allowed)

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

                                1 Reply Last reply
                                0

                                • Login

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