나는 내 TreeItem
제외 열 == 1의 인스턴스를 채울 모델QAbstractItemModel에서 QCheckBox를 설정하는 방법은 무엇입니까?
class TreeModel : public QAbstractItemModel
있습니다.
QVariant TreeModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole) {
if (role == Qt::CheckStateRole) {
if (index.column() == 1) {
if (index.row() == 1) {
return Qt::Unchecked;
} else
return Qt::Checked;
}
}
return QVariant();
}
if (role == Qt::DisplayRole) {
if (index.column() != 1) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
return item->data(index.column());
}
}
return QVariant();
}
내가 Qt::Checked
또는 Qt::Unchecked
이러한 확인란에게 동상을 설정할 수 있지만 내 문제는 다음과 같습니다 : 1 열에서 내가 CheckBoxes
을 만든 그들은를 클릭 할 때 은 나중에 변경할 수 없습니다 (그러나 setData
가 호출됩니다 적절한 index.column==1
및 role==Qt::CheckStateRole
). ItemDelegate
으로 예를 보았습니다. 사실입니까? 이 시나리오에서는 대리인을 사용해야합니까?
bool TreeModel::setData(const QModelIndex & index, const QVariant & value,
int role) {
if (role==Qt::CheckStateRole && index.column() == 1) {
TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer());
//if (item->data(index.column()) == Qt::Checked)
if (value == Qt::Checked){
int i=1;
//check->setCheckState(1,Qt::Checked); //SIGSEGV
}else{
//(item->data(index.column())) = Qt::Unchecked;
int i=2;
//check->setCheckState(1,Qt::Unchecked);
}
emit dataChanged(index, index);
return true;
}
emit dataChanged(index, index);
return true;;
}
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEditable;
}
그래서, 내가 한 말) – Trompa
대단히 또한 감사합니다. setData()에 데이터를 저장하는 것은 내가 놓친 것이다. – 4pie0