[Qt-interest] Select all indices in a QFileSystemModel ?
Stephen Collyer
scollyer at netspinner.co.uk
Thu Jun 18 19:11:30 CEST 2009
I have a QTreeView whose model is a QFileSystemModel.
I want to be able to select all the files in the model. I have some
somewhat clunky code that tries to do this:
void CreateTransactionDlg::select_all_files()
{
set_all_files_selection_state(QItemSelectionModel::Select,
root_index_);
}
void CreateTransactionDlg::set_all_files_selection_state(const
QItemSelectionModel::SelectionFlags flags,
const QModelIndex&
parent)
{
// select all at current level
int row_count = dir_model_->rowCount(parent) - 1;
QModelIndex top_left = dir_model_->index(0, 0, parent);
QModelIndex bottom_right = dir_model_->index(row_count, 0, parent);
QItemSelection selection(top_left, bottom_right);
QItemSelectionModel* selection_model = files_tree->selectionModel();
selection_model->select(selection, flags);
// recurse over children
foreach (QModelIndex i, selection_model->selectedIndexes())
{
if (dir_model_->hasChildren(i) && i.parent() == parent)
{
set_all_files_selection_state(flags, i);
}
}
}
This works partially, in that it selects all files which are currently
displayed in the view. If I expand the whole view to show the
contents of subdirs, it selects everything visible. If I don't,
it selects only those visible files.
It's not clear to me what is preventing the code from selecting
everything in the model. Can someone explain how to fix this ?
--
Stephen Collyer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090618/c57f473d/attachment.html
More information about the Qt-interest-old
mailing list