PDA

View Full Version : Method selectAll() from QListView does not work properly



Treadstone
14th August 2010, 12:08
It's my first post here, so I would say hello to you!

I've got problem with QListView and QFileSystemModel. I've created a model:


model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());

and connected it with listView:


ui->listView->setModel(model);
ui->listView->setRootIndex(model->index(tr("/home/lukasz/")));

Everything works good but one thing: selecting all of items. When I click ctrl+a, or invoke method selectAll():

ui->listView->selectAll();

method ui->listView->selectionModel()->selectedIndexes() returns number of highlighted files multipled by four. For example when I select manually all, four items, method selectedIndexes() returns number 4. But when I click ctrl+a or invoke selectAll(), selectedIndexes() returns 16.
I tried compiling on linux and on windows too - results are the same.
Here is a very simple project created with Qt Creator: link (http://treadstone.sh.dug.net.pl/badselect.rar). It should help you, if solution of my problem isn't simple.

Regards

Lykurg
14th August 2010, 13:14
Use selectedRows() and you get what you want. With selectedIndexes() you get all selected indexes and since your model has 4 columns (put it in a QTableView or QTreeView and you will see) you always get the x4.

Treadstone
16th August 2010, 13:57
Thanks for response. So only elegant way to do this is create new, custom model? If I use selectedRows(), it works properly only with selectAll() method (selecting manually items does not work)

Lykurg
16th August 2010, 14:05
So only elegant way to do this is create new, custom model?No, all works well with Qt stuff. And the model has nothing to do with the selection.
If I use selectedRows(), it works properly only with selectAll() method (selecting manually items does not work)
what does not work exactely? Are you displaying all columns? Also you might want use proper selectionBehavior and selectionMode settings. (Or you can remove the non needed columns from the model.)

Treadstone
16th August 2010, 20:55
Sorry, I didn't unterstood it very well, but now everything with selecting behavior is clear. Could you easily explain me how to remove columns in that model?

Lykurg
16th August 2010, 21:00
Use QAbstractItemModel::removeColumn() to remove non needed columns. It will save memory and provides better performance.

Treadstone
17th August 2010, 01:43
I dont know how to use it; especially what to give as a second argument, QModelIndex. I'm pretty sure it cant be be a single item from the model, but dont have any ideas. Can you explain me it a bit more accurately or simply write a small piece of code which will remove single column?

thanks in advance

Lykurg
17th August 2010, 07:39
I see right know that QFileSystemModel does not reimp removecolumns, so you can't use it. Then you have to use the selection options.

Treadstone
19th August 2010, 12:18
Thanks, used selectedRows() and it's working fine :)

Regards