Click a directory in listWidget and display images on another listWidget



  • Hi there . I am trying to display images in separate widget when I choose a directory from another listWidget. In that case I don't know how can I click a directory and do something . I mean how can I connect a click action on a directory and send out put in another widget.(just 1 click on a dir(in separate widget) and display images (inside this selected dir) on another listWidget. Here is my current code.

    QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",                                                                                                            QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        
        for(const QFileInfo & finfo: directory.entryInfoList()){
            ui->listWidget->addItem(finfo.absoluteFilePath());
        }
        
        
        
       ("selecteditem"from listWidget).setNameFilters({"*.png", "*.jpg"});
        
        for(const QFileInfo & finfo: ("selecteditem"from listWidget).entryInfoList()){
            QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
            ui->listWidget_2->addItem(item);
        }
    

    I think I need signal and slot .


  • Qt Champions 2017

    Hi @Kinesis,

    The QListWidget signals are documented here: http://doc.qt.io/qt-5/qlistwidget.html#signals

    I seems you are looking for itemClicked. In your slot you can fill the second list widget with your images.

    Regards



  • @aha_1980
    I tried my code as U said but i don't know how can I get clicked directory in listWidget(directory) to list in listWidget(images).I also need to filter image files .Please show me the way. Here is the modified code.

    for(const QFileInfo & finfo: directory.entryInfoList()){
            ui->listWidget->addItem(finfo.absoluteFilePath());
        }
    
    
        connect(ui->listWidget, & QListWidget::itemClicked,[listWidget_2,this](QListWidgetItem *item)
        {
           item.setNameFilters({"*.png", "*.jpg"});
            
            for(const QFileInfo & finfo: item.entryInfoList()){
                QListWidgetItem *img = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
                listWidget_2->addItem(img);
           
    
                }
        });
    

  • Qt Champions 2017

    @Kinesis said in Click a directory in listWidget and display images on another listWidget:

    Ok, looks good so far.

    In your slot you have *item which is the currently clicked item. By using item->text() you get the directory name from listWidget.

    For putting images in a list you should use Google ;) I have no ready solution for you now.

    Regards



  • @aha_1980
    Thanks . I will try to figure it more .



  • I got the answer.

    QDir directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home",
                                                           
        QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks);
        auto listWidget_images = new QListWidget();//set listwidget to display images
        listWidget_images->setMinimumSize(1200,400);
        listWidget_images->setViewMode(QListWidget::IconMode);
        listWidget_images->setIconSize(QSize(320,240));
        listWidget_images->setResizeMode(QListWidget::Adjust);
        
        for(const QFileInfo & finfo: directory.entryInfoList()){
            ui->listWidget_dirs->addItem(finfo.absoluteFilePath());
        }
       
        
        connect(ui->listWidget_dirs, & QListWidget::itemClicked,[listWidget_images,this](QListWidgetItem *item)
        {
            listWidget_images->show();
            listWidget_images->clear();
            QDir directory(item->text());
            directory.setNameFilters({"*.png", "*.jpg"});
            for(const QFileInfo & finfo: directory.entryInfoList()){
                QListWidgetItem *item = new QListWidgetItem(QIcon(finfo.absoluteFilePath()), finfo.fileName());
                listWidget_images->addItem(item);
            }
        });
    


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