View Full Version : Move multiple selected items from Qtreeview to QListBox
sargeslash
31st January 2013, 14:24
I want to move selected items from QTreeview (populated by QFileSystemModel) to a QListBox. I have managed to get all selected items in a QMdelIndex list, but I cant add this to QListbox since QListBox->addItems(QStringList) only accepts QStringList.
Please guide me through.
QItemSelectionModel selectiveModel=ui->treeView->selectionModel();
QModelIndexList list=selectiveModel.selectedIndexes();
ui->inputImageLstBox->addItems(list);
Santosh Reddy
31st January 2013, 14:47
QTreeView * treeView;
QListWidget * listBox;
QModelIndexList list = treeView->selectionModel()->selectedIndexes();
QStringList labels;
foreach(QModelIndex index, list)
labels << index.data().toString();
listBox->addItems(labels);
sargeslash
31st January 2013, 14:57
I tried the code, is here any possibility to add any spfic column to the listbox?
As right now I when I am adding all the coulmns of the QTreeView are added (i.e. column 0,1,2...)
Since column 1 only represents name so I only want to add this(column 1) to the Listbox.
Output looks like this:8660
Santosh Reddy
31st January 2013, 15:01
Yes, just check the column number of the index
foreach(QModelIndex index, list)
if(index.column() == 1)
labels << index.data().toString();
sargeslash
31st January 2013, 15:40
You made my day :)
Thats so awesome
Added after 35 minutes:
This was really nice help but now it causes some troubles in my application,
Since before this I was doing this
QModelIndex index = ui->treeView->currentIndex();
QListWidgetItem *item;
item = new QListWidgetItem;
item->setData(Qt::DisplayRole, model->fileName(index));
item->setData(Qt::UserRole, model->filePath(index));
and then saving the items by
outputFile.write(ui->inputImageLstBox->model()->index(i, 0).data(Qt::UserRole).toString().toStdString().c_s tr());
But now by using the above concept of selecting multiple files how can I use the filePath for all the selectedIndexes?
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.