1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #ifndef CUSTOMMODEL_H
- #define CUSTOMMODEL_H
- #include "QtWidgets/qtreeview.h"
- #include <QAbstractItemModel>
- #include <QMouseEvent>>
- struct ListItem
- {
- QString text; // 用于显示的文本
- QList<ListItem> children; // 子项列表
- bool operator==(const ListItem& other) const
- {
- return text == other.text && children == other.children;
- }
- };
- class CustomModel : public QAbstractItemModel
- {
- Q_OBJECT
- public:
- explicit CustomModel(QObject* parent = nullptr);
- ~CustomModel();
- // 重写必要的函数
- QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
- QModelIndex parent(const QModelIndex& child) const override;
- int rowCount(const QModelIndex& parent = QModelIndex()) const override;
- int columnCount(const QModelIndex& parent = QModelIndex()) const override;
- QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
- Qt::ItemFlags flags(const QModelIndex& index) const override;
- bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
- bool insertTopLevelItem(int row, const ListItem& item);
- bool insertChildItem(const QModelIndex& parent, int row, const ListItem& item);
- bool removeTopLevelItem(int row);
- bool removeChildItem(const QModelIndex& parent, int row);
- ListItem getItem(const QModelIndex& index) const;
- private:
- // 自定义数据结构用于存储一级列表和二级列表的数据
- QList<ListItem> topLevelItems;
- };
- #endif // CUSTOMMODEL_H
|