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. Click a directory in listWidget and display images on another listWidget

Click a directory in listWidget and display images on another listWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5.0listwidgetsignal & slotimages
6 Posts 2 Posters 2.9k 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.
  • K Offline
    K Offline
    Kinesis
    wrote on 12 Jul 2018, 04:42 last edited by Kinesis 7 Dec 2018, 04:45
    #1

    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 .

    A 1 Reply Last reply 12 Jul 2018, 05:31
    0
    • K Kinesis
      12 Jul 2018, 04:42

      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 .

      A Offline
      A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on 12 Jul 2018, 05:31 last edited by
      #2

      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

      Qt has to stay free or it will die.

      K 1 Reply Last reply 12 Jul 2018, 07:44
      4
      • A aha_1980
        12 Jul 2018, 05:31

        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

        K Offline
        K Offline
        Kinesis
        wrote on 12 Jul 2018, 07:44 last edited by
        #3

        @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);
               
        
                    }
            });
        
        A 1 Reply Last reply 12 Jul 2018, 07:54
        0
        • K Kinesis
          12 Jul 2018, 07:44

          @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);
                 
          
                      }
              });
          
          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 12 Jul 2018, 07:54 last edited by
          #4

          @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

          Qt has to stay free or it will die.

          K 1 Reply Last reply 12 Jul 2018, 08:03
          1
          • A aha_1980
            12 Jul 2018, 07:54

            @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

            K Offline
            K Offline
            Kinesis
            wrote on 12 Jul 2018, 08:03 last edited by
            #5

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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kinesis
              wrote on 16 Jul 2018, 07:51 last edited by
              #6

              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);
                      }
                  });
              
              1 Reply Last reply
              1

              3/6

              12 Jul 2018, 07:44

              • Login

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