QStyledItemDelegate

view 中的编辑功能默认实现下是在该单元格处于编辑状态时才会实例化编辑组件,相关接口如下:

1 QWidget *createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index) const override;
2 void setEditorData(QWidget *editor, const QModelIndex &index) const override;
3 void setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const override;
4 void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex &index) const override;

当双击进入编辑状态时,

调用 createEditor 在目标位置创建一个编辑组件,

通过 setEditorData 更新编辑组件的数据,

编辑完成后调用 setModelData 将结果设置回 model 中。

由于源码太长,只看下创建是如何实现的:

 1 QWidget *QStyledItemDelegate::createEditor(QWidget *parent,
 2                                      const QStyleOptionViewItem &,
 3                                      const QModelIndex &index) const
 4 {
 5     Q_D(const QStyledItemDelegate);
 6     if (!index.isValid())
 7         return nullptr;
 8     return d->editorFactory()->createEditor(index.data(Qt::EditRole).userType(), parent);
 9 }
10  
11 QWidget *QDefaultItemEditorFactory::createEditor(int userType, QWidget *parent) const
12 {
13     switch (userType) {
14 #if QT_CONFIG(combobox)
15     case QMetaType::Bool: {
16         QBooleanComboBox *cb = new QBooleanComboBox(parent);
17         cb->setFrame(false);
18         cb->setSizePolicy(QSizePolicy::Ignored, cb->sizePolicy().verticalPolicy());
19         return cb; }
20 #endif
21 #if QT_CONFIG(spinbox)
22     case QMetaType::UInt: {
23         QSpinBox *sb = new QUIntSpinBox(parent);
24         sb->setFrame(false);
25         sb->setMinimum(0);
26         sb->setMaximum(INT_MAX);
27         sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
28         return sb; }
29     case QMetaType::Int: {
30         QSpinBox *sb = new QSpinBox(parent);
31         sb->setFrame(false);
32         sb->setMinimum(INT_MIN);
33         sb->setMaximum(INT_MAX);
34         sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
35         return sb; }
36 #endif
37 #if QT_CONFIG(datetimeedit)
38     case QMetaType::QDate: {
39         QDateTimeEdit *ed = new QDateEdit(parent);
40         ed->setFrame(false);
41         return ed; }
42     case QMetaType::QTime: {
43         QDateTimeEdit *ed = new QTimeEdit(parent);
44         ed->setFrame(false);
45         return ed; }
46     case QMetaType::QDateTime: {
47         QDateTimeEdit *ed = new QDateTimeEdit(parent);
48         ed->setFrame(false);
49         return ed; }
50 #endif
51 #if QT_CONFIG(label)
52     case QMetaType::QPixmap:
53         return new QLabel(parent);
54 #endif
55 #if QT_CONFIG(spinbox)
56     case QMetaType::Double: {
57         QDoubleSpinBox *sb = new QDoubleSpinBox(parent);
58         sb->setFrame(false);
59         sb->setMinimum(-DBL_MAX);
60         sb->setMaximum(DBL_MAX);
61         sb->setSizePolicy(QSizePolicy::Ignored, sb->sizePolicy().verticalPolicy());
62         return sb; }
63 #endif
64 #if QT_CONFIG(lineedit)
65     case QMetaType::QString:
66     default: {
67         // the default editor is a lineedit
68         QExpandingLineEdit *le = new QExpandingLineEdit(parent);
69         le->setFrame(le->style()->styleHint(QStyle::SH_ItemView_DrawDelegateFrame, nullptr, le));
70         if (!le->style()->styleHint(QStyle::SH_ItemView_ShowDecorationSelected, nullptr, le))
71             le->setWidgetOwnsGeometry(true);
72         return le; }
73 #else
74     default:
75         break;
76 #endif
77     }
78     return nullptr;
79 }

参考资料:https://blog.csdn.net/gongjianbo1992/article/details/108687172

posted @ 2025-12-11 18:50  家煜宝宝  阅读(0)  评论(0)    收藏  举报