Results 1 to 2 of 2

Thread: QModelIndexList: getting 4 QModelIndex for each item selected in a QTreeView.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default QModelIndexList: getting 4 QModelIndex for each item selected in a QTreeView.

    In the following code, why am I getting size 4 for QModelIndexList if I click in one file or directory only? How can I get only one path? I know I could use [0] but is there another way to get size 1?

    Qt Code:
    1. // fsModel = QFileSystemModel
    2. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    3. {
    4. ui->setupUi(this);
    5. const QString rootPath = "/home/rob/";
    6.  
    7. fsModel = new QFileSystemModel(this);
    8. fsModel->setRootPath(rootPath);
    9. ui->treeView->setModel(fsModel);
    10. ui->treeView->setRootIndex(fsModel->index(rootPath));
    11. }
    12.  
    13. void MainWindow::on_buttonCompress_clicked()
    14. {
    15. QModelIndexList list = ui->treeView->selectionModel()->selectedIndexes();
    16.  
    17. qDebug() << list.size();
    18.  
    19. for (const QModelIndex &modelIndex: list)
    20. qDebug() << fsModel->filePath(modelIndex);
    21.  
    22. // I wouldn't like to use []. I would like to get only one filepath, the one I selected.
    23. qDebug() << fsModel->filePath(list[0]);
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModelIndexList: getting 4 QModelIndex for each item selected in a QTreeView.

    What are you using for your QTreeView::selectionBehavior()? SelectRows? If so, the selection model will return all QModelIndex items for each column in the row.

    If you examine the QModelIndex entries in the list, I think you should see that they all have the same row() and parent() but different column() values.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 25th January 2015, 05:23
  2. Replies: 2
    Last Post: 25th December 2014, 14:27
  3. Replies: 3
    Last Post: 4th January 2012, 00:44
  4. How to keep selected item in QTreeView
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2011, 06:16
  5. Changing selected QTreeView item font
    By dima1000 in forum Newbie
    Replies: 5
    Last Post: 8th October 2009, 10:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.