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. Getting full path from QListWidget
Forum Update on Tuesday, May 27th 2025

Getting full path from QListWidget

Scheduled Pinned Locked Moved General and Desktop
qlistwidget
5 Posts 3 Posters 2.8k Views 1 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.
  • A Offline
    A Offline
    Aashu10
    wrote on last edited by
    #1

    have 2 listwidgets, lets call them listwidgetinput and listwidgetoutput. I have alot of files(only file name) on listwidgetinput. And i trim the file name before adding it to listwidgetinput like this :

    QString Dir, Type;
    QStringList Files;
    Qlistwidget wid
    
    if (index==0)
      {
        Dir.append(C:\desktop....);
        type.append(".txt")
        wid = ui->listwidgetinput_txt;
        }
    if (index ==1)
      {
        Dir.append(C:\desktop....);
        type.append(".doc")
        wid = ui->listwidgetinput_doc
        }
    QDirIterator it(Dir, QStringList() << Type, QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext())
        {
            auto item = new QListWidgetItem;
            it.next();
            item->setText(it.fileName());
            item->setData(Qt::UserRole, it.filePath())
             myWidget->addItem(item);
          }
    wid->additems(Files);
    

    i want to retireve the file path of all items in listwidgetoutput. I insert items to listwidgetoutput like this:

    QList <QListWidgetItem*> items=ui->listWidgetinput->selectedItems();
    for(int j=0;j<items.count();j++)
    {
    list= items.at(j)->text();
    ui->listWidgetOutput->insertItem(j,list);``
    
    How do i retrieve the path of the files in listwidgetoutput
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Since you store the path in the Qt::UserRole you can get it back from there too i.e.

      for(auto item: items)
      {
          auto filename = item->text(); // this is shortcut for: item->data(Qt::DisplayRole).toString()
          auto path     = item->data(Qt::UserRole).toString();
          ...
      }
      
      1 Reply Last reply
      1
      • A Offline
        A Offline
        Aashu10
        wrote on last edited by
        #3

        Qt::userRole holds path of all files in listwidgetinput, only selected files are transfered to listwidgetoutput. I was confused on how to get path of only selected files.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          One thing which I saw while adding elements to output list, you are lonely adding the text. That means you are NOT adding the item from input list but just text from each item. This may be giving problem for you. Instead of inserting the text() insert the complete item from inputList to output list. After this you can iterate through items from ListwidgetOutput and fetch user role data as well.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          4
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            So do you want all paths or only the selected in input ones?
            For selected you would do:

            auto items = ui->listWidgetinput->selectedItems();
            for(auto item : items)
            {
                auto path = item->data(Qt::UserRole).toString();
                ui->listWidgetOutput->addItem(path);
            }
            

            For all you would do:

            auto numItems  = ui->listWidgetinput->count();
            for(int i = 0; i < numItems; ++i)
            {
                auto path = ui->listWidgetinput->item(i)->data(Qt::UserRole).toString();
                ui->listWidgetOutput->addItem(path);
            }
            
            1 Reply Last reply
            2

            • Login

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