How to change the checkbox size of QFileSystemModel and custom checkbox shape
Unsolved
General and Desktop
-
Declare it as subclassing in a class that inherits from QFileSystemModel and give it a checkbox property.
The result was successful. However, it is a basic check box provided by qt and the size is also too small. I want to grow a checkbox and change it to a checkbox I have defined.class usrFileSystemModel : public QFileSystemModel { Q_OBJECT private: mutable QMap <qint64, Qt::CheckState> m_CheckedItems; public: usrFileSystemModel(QObject *parent); ~usrFileSystemModel(); // [ SubClassing ] virtual bool setData(const QModelIndex& index, const QVariant& value, int role); virtual Qt::ItemFlags flags(const QModelIndex& index) const; virtual QVariant data(const QModelIndex& index, int role)const; signals: void itemChecked(const QModelIndex&); protected slots: void onItemChecked(const QModelIndex& index); }; #include "usrFileSystemModel.h" usrFileSystemModel::usrFileSystemModel(QObject *parent) : QFileSystemModel(parent) { this->connect(this, SIGNAL(itemChecked(const QModelIndex&)), SLOT(onItemChecked(const QModelIndex&))); } usrFileSystemModel::~usrFileSystemModel() { } bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (role == Qt::CheckStateRole && index.column() == 0) { m_CheckedItems[index.internalId()] = static_cast<Qt::CheckState>(value.toInt()); emit itemChecked(index); emit dataChanged(index, index.sibling(0, 0)); return true; } return QFileSystemModel::setData(index, value, role); } Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const { return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable; } QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const { if (role == Qt::CheckStateRole && index.column() == 0) return QVariant(m_CheckedItems[index.internalId()]); return QFileSystemModel::data(index, role); } void usrFileSystemModel::onItemChecked(const QModelIndex& index) { Qt::CheckState state = m_CheckedItems[index.internalId()]; fetchMore(index); if (state == Qt::Checked || state == Qt::Unchecked) { for (int i = 0; i < rowCount(index); i++) { QModelIndex child = index.child(i, 0); if (m_CheckedItems[child.internalId()] != state) setData(child, state, Qt::CheckStateRole); } } QModelIndex parent = index.parent(); if (parent.isValid()) { state = m_CheckedItems[parent.child(0, 0).internalId()]; if (state == Qt::PartiallyChecked) m_CheckedItems[parent.internalId()] = state; else { int i = 1; while (i < rowCount(parent) && m_CheckedItems[parent.child(i, 0).internalId()] == state) i++; if (i != rowCount(index)) state = Qt::PartiallyChecked; m_CheckedItems[parent.internalId()] = state; } } }