How to show/hide checkboxes “on the fly” on a QFileSystemModel?
-
I work on QFileSystemModel to make it more flexible.
I've set the directory/file checkable Setted empty dirs non-collapsable Added the support of Tri-state to _PartiallyCheck_ parents of checked elements Show/Hide "on the fly" PartiallyChecks ...
But a this point, i block on a behavior.
I want to show/hide "Checked/Unchecked" checkboxes on the fly
So, I've edit my flag method:
@Qt::ItemFlags FSModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flag = QFileSystemModel::flags(index);// set flag to have only first column checkable if ((index.column() == 0) && (m_user_checkable)) { flag |= Qt::ItemIsUserCheckable; } return flag;
}@
note: m_user_checkable is a boolean which active/desactive checkboxes for the model.
I've put "@qDebug() << filePath(index) << index.flags();@" inside my handler, for outputs flags of the index where I click, to see his value before and after "m_user_checkable" changes. So, I'm sure that index flags change but the checkboxes stay visibles :/
Did you know a way to hide these checkboxes once they are showed ?
PS: ask if you want the whole code here... (~ 500 lines)
Thanks
-
your model needs to tell the view that something has changed. Meaning tell the view that a item's checkbox isn't available anymore.
You may try to trigger QAbstractItemModel::dataChanged() signal when a checkbox for an index isn't available anymore.
Then the view should regather the data from the model...Alternatively you can call QAbstractItemModel::reset() if the checkboxes for all indexes changes.
-
It sounds to me you are looking for "something like this":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox ?
-
Not really, my code already provides all these features.
Now, I want to build a function which show/hide checkboxes once tree is already rendered.
Imagine the situation: you have a button, and if you click it, it call the fonction that show or hide checkboxes of the list.
-
[quote author="raven-worx" date="1375442462"]your model needs to tell the view that something has changed. Meaning tell the view that a item's checkbox isn't available anymore.
You may try to trigger QAbstractItemModel::dataChanged() signal when a checkbox for an index isn't available anymore.
Then the view should regather the data from the model...Alternatively you can call QAbstractItemModel::reset() if the checkboxes for all indexes changes.[/quote]
Sry, i wasn't pay attention to your message before...
I had already added "dataChanged()", but your message gave me indirectly the solution...dataChanged... Data changed... hum, data had changed? Oh my god, the datas have changed...
I hadn't added the condition in the data() method
@QVariant FSModel::data(const QModelIndex& index, int role) const
{
if (index.isValid()) {
// HERE, i added "m_user_checkable && " before "role == Qt::CheckSt..."
if (m_user_checkable && role == Qt::CheckStateRole && index.column() == 0) {
// item checked only if we have stored its path
if (m_checked.contains(filePath(index))) {
return Qt::Checked;
}
if (m_partially_check_parents && m_partially_checked.contains(filePath(index))) {
return Qt::PartiallyChecked;
}
return Qt::Unchecked;
}
}
return QFileSystemModel::data(index, role);
}@Just this boolean... now it works perfectly :p