{
...
private:
QSet<QString> checked; // wysota's suggestion
}
Qt::ItemFlags MyDirModel::flags(const QModelIndex& index) const
{
if (index.column() == 0) // make the first column checkable
f |= Qt::ItemIsUserCheckable;
return f;
}
QVariant MyDirModel
::data(const QModelIndex
& index,
int role
= Qt
::DisplayRole) const {
if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
{
// the item is checked only if we have stored its path
return (checked.contains(filePath(index)) ? Qt::Checked : Qt::Unchecked);
}
}
bool MyDirModel::setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)
{
if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
{
// store checked paths, remove unchecked paths
if (value.toInt() == Qt::Checked)
checked.insert(filePath(index));
else
checked.remove(filePath(index));
return true;
}
return QDirModel::setData(index, value, role
);
}
class MyDirModel : public QDirModel
{
...
private:
QSet<QString> checked; // wysota's suggestion
}
Qt::ItemFlags MyDirModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags f = QDirModel::flags(index);
if (index.column() == 0) // make the first column checkable
f |= Qt::ItemIsUserCheckable;
return f;
}
QVariant MyDirModel::data(const QModelIndex& index, int role = Qt::DisplayRole) const
{
if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
{
// the item is checked only if we have stored its path
return (checked.contains(filePath(index)) ? Qt::Checked : Qt::Unchecked);
}
return QDirModel::data(index, role);
}
bool MyDirModel::setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)
{
if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole)
{
// store checked paths, remove unchecked paths
if (value.toInt() == Qt::Checked)
checked.insert(filePath(index));
else
checked.remove(filePath(index));
return true;
}
return QDirModel::setData(index, value, role);
}
To copy to clipboard, switch view to plain text mode
Bookmarks