Код
Тестовый виджет прикреплен.C++ (Qt)Qt::ItemFlags CFileSystemModel::flags(const QModelIndex &index) const
{
return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEditable
| Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
}
QVariant CFileSystemModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
else if(role == Qt::CheckStateRole)
{
return checkedIndexes.contains(index) ? Qt::Checked : Qt::Unchecked;
}
else if (role == Qt::DisplayRole && fileInfo(index).isFile() )
{
return m_needFilenameWithExt ? fileInfo(index).fileName() : fileInfo(index).baseName();
}
else if (role == Qt::EditRole)
{
return fileInfo(index).baseName();
}
else
{
return QFileSystemModel::data(index, role);
}
}
bool CFileSystemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(index.isValid() && role == Qt::CheckStateRole)
{
if(value == Qt::Checked)
{
checkedIndexes.insert(index);
if(hasChildren(index) == true)
{
recursiveCheck(index, value);
}
}
else
{
checkedIndexes.remove(index);
if(hasChildren(index) == true)
{
recursiveCheck(index, value);
}
}
emit dataChanged(index, index);
return true;
}
else if (index.isValid() && role == Qt::EditRole && fileInfo(index).isFile())
{
m_needFilenameWithExt = true;
bool result = QFileSystemModel::setData(index, value.toString() + "." + fileInfo(index).completeSuffix(), role);
m_needFilenameWithExt = false;
return result;
}
return QFileSystemModel::setData(index, value, role);
}
bool CFileSystemModel::recursiveCheck(const QModelIndex &index, const QVariant &value)
{
if(hasChildren(index))
{
int i;
int childrenCount = rowCount(index);
QModelIndex child;
for(i=0; i<childrenCount; i++)
{
child = QFileSystemModel::index(i, 0, index);
setData(child, value, Qt::CheckStateRole);
}
}
}
Что не нравится на данный момент:
- Если отметить папку, отмечаются рекурсивно все подкаталоги и их содержимое. Необходимо, что бы отмечались только файлы корневого каталога.
- Реализовать тройной чекбокс с неопределенным состоянием.
И соответственно сам вопрос, как доработать минимальными силами? Вопрос решаем, но как показала практика лучше поучиться у профи, чем днями ковырять код.
P.S. Один вопрос вопрос снят с повестки. Первый пока недорабтан.
По поводу третьего. Флажок то есть, но видимо его установки не достаточно.