Я настроил свою модель данных AnnotationListModel, унаследованную от QStringListModel.
Целью класса AnnotationListModel является добавление флажков для элементов данных и достижение исключения между элементами данных.
Теперь я создаю три объекта QListView и AnnotationListModel в QDialog. Элемент данных в модели добавляется в виде флажка и является исключительным.
Я могу установить флажок в QListView, но статус «Отмечено» не может быстро отобразиться.
Как включить быструю очистку QListView?
класс AnnotationListModel: public QStringListModel { Q_OBJECT публика: AnnotationListModel (AnnotationType annType, const QStringList& stringList, QObject* родительский = nullptr); ~AnnotationListModel() {}; защищено: Флаги Qt::ItemFlags(const QModelIndex& index) const переопределение; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; сигналы: void sendAnnotation (const AnnotationType& annoType, const QString& аннотация); частный: Тип аннотации m_annoType; QStringList m_stringList; QMap m_stringList_map; // }; AnnotationListModel::AnnotationListModel(AnnotationType annType, const QStringList& stringList, родительский объект QObject*) :QStringListModel(родительский), m_stringList(stringList), m_annoType(annType) { setStringList(m_stringList); for (авто и значение: m_stringList) { m_stringList_map[значение] = ложь; } } Qt::ItemFlags AnnotationListModel::flags(const QModelIndex& index) const { если (!index.isValid()) { вернуть Qt::ItemIsEnabled; } вернуть Qt::ItemIsUserCheckable | QStringListModel::flags(index); } QVariant AnnotationListModel::data(const QModelIndex& index, int role) const { если (!index.isValid()) { вернуть QVariant(); } если (роль == Qt::CheckStateRole) { if (m_stringList_map[m_stringList.at(index.row())] == true) { вернуть Qt::Проверено; } еще { вернуть Qt::Unchecked; } } иначе, если (роль == Qt::DisplayRole) { return m_stringList.at(index.row()); } вернуть QStringListModel::data(индекс, роль); } bool AnnotationListModel::setData(const QModelIndex& индекс, константное значение QVariant&, роль int) { если (!index.isValid()) { вернуть ложь; } если (роль == Qt::CheckStateRole) { если (значение == Qt::Проверено) { for (int i{0}; i listView_country->setModel(m_counCodeModel); Connect(m_counCodeModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation); перерыв; случай AnnotationType::DriSide: m_driSideModel = новая AnnotationListModel (AnnotationType::DriSide, l_annoStrList); m_uiDialog-> listView_driving-> setModel (m_driSideModel); Connect(m_driSideModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation); перерыв; случай AnnotationType::RoadType: m_roadTypeModel = новая AnnotationListModel (AnnotationType::RoadType, l_annoStrList); m_uiDialog->listView_road->setModel(m_roadTypeModel); Connect(m_roadTypeModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation); } Диалог открывается через mainwindow.
