[solved]Setting Images in QListWidget



  • 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

    @


  • Lifetime Qt Champion

    Hi,

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



  • 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?


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    Not just the same connect statement, the complete constructor code



  • @

    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_; @


  • Lifetime Qt Champion

    You don't show listObject_.

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



  • 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?


  • Lifetime Qt Champion

    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.



  • 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();

    @



  • Hi,

    Solved this problem using the following in

    ImageRegistration constructor

    @
    listWidget *listObject=ui->ImageList;
    @


  • Lifetime Qt Champion

    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)


Log in to reply
 

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