[SOLVED]selectedIndexes returns duplicates



  • Hi ,
    i have a treeview, inside it i have QFileSystemModel ,
    in the treeview i allow :
    @
    ui->treeView->setSelectionMode(QAbstractItemView::MultiSelection);
    @

    This is the code to read the selections :

    @
    QStringList paths;
    QModelIndexList indexes_list = ui->treeView->selectionModel()->selectedIndexes();
    for(QModelIndexList::iterator it = indexes_list.begin();it != indexes_list.end();
    it++){
    paths.append(files->fileInfo((*it)).absoluteFilePath());
    }
    path = paths.join(";");
    @

    From the treeview i went to a directory where there is only two files and selected them using Ctrl+left mouse click .
    when the above code is reached i get 4 duplicates from each file path inside the :
    @
    QStringList paths;
    @

    above .

    what am i doing wrong ?


  • Moderators

    A row in the QFileSystemModel has 4 columns (name, size, type, time). So when selecting a line you receive 4 selected model indexes.

    So it depends what you code exactly does in the line
    @
    files->fileInfo((*it)
    @

    You should only process model indexes of the column 0 in your case.



  • files is my QFileSystemModel , how do i read only the first column ?


  • Moderators

    @
    if( (*it).column() == 0 )
    paths.append(files->fileInfo((*it)).absoluteFilePath());
    @



  • thanks a lot it worked .


Log in to reply
 

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