Qt自定义委托实现定制输入的效果 Qt自定义委托实现定制输入的效果参考博客link 。以下是根据这篇博客修改而来。并写上了自己在编写过程中觉得应该注意的地方。widget.cpp#includewidget.h#includeui_widget.hWidget::Widget(QWidget*parent):QWidget(parent),ui(newUi::Widget){ui-setupUi(this);QVBoxLayout*vboxnewQVBoxLayout;QTreeView*treeViewnewQTreeView;myDelegate*delegatenewmyDelegate;QStandardItemModel*modelnewQStandardItemModel;//QTreeView常用的设置treeView-setSelectionMode(QAbstractItemView::SingleSelection);treeView-setSelectionBehavior(QAbstractItemView::SelectItems);treeView-setAlternatingRowColors(true);//设置标题model-setHorizontalHeaderLabels(QStringList()QStringLiteral(年级/班级)QStringLiteral(姓名)QStringLiteral(分数)QStringLiteral(评价));//表头相关的设置treeView-header()-setStretchLastSection(true);//最后一列自适应宽度//往model里面添加数据//一级节点只有 班级列的数据for(inti0;i5;i){QStandardItem*itemnewQStandardItem(QStringLiteral(%1年级).arg(i1));model-appendRow(item);for(intj0;j5;j){QStandardItem*item1_0newQStandardItem(QStringLiteral(%1班).arg(j1));QStandardItem*item1_1newQStandardItem(QStringLiteral(Tom));QStandardItem*item1_2newQStandardItem(QStringLiteral(100));QStandardItem*item1_3newQStandardItem(QStringLiteral(差));item-appendRow(QListQStandardItem*()item1_0item1_1item1_2item1_3);}}treeView-setModel(model);treeView-setItemDelegate(delegate);vbox-addWidget(treeView);setLayout(vbox);}Widget::~Widget(){deleteui;}widget.h#ifndefWIDGET_H#defineWIDGET_H#includeQWidget#includeQVBoxLayout#includeQTreeView#includeQStandardItemModel#includemydelegate.hnamespaceUi{classWidget;}classWidget:publicQWidget{Q_OBJECTpublic:explicitWidget(QWidget*parentnullptr);~Widget();private:Ui::Widget*ui;};#endif// WIDGET_Hmydelegate.cpp#includemydelegate.hmyDelegate::myDelegate(QObject*parent):QStyledItemDelegate(parent){}QWidget*myDelegate::createEditor(QWidget*parent,constQStyleOptionViewItemoption,constQModelIndexindex)const{Q_UNUSED(option);//只有第三列的item才使用qcombox进行编辑if(index.column()3){QComboBox*boxnewQComboBox(parent);box-addItems(QStringList()QStringLiteral(优)QStringLiteral(良)QStringLiteral(差));returnbox;}returnnullptr;}voidmyDelegate::setEditorData(QWidget*editor,constQModelIndexindex)const{QString strindex.data(Qt::EditRole).toString();QComboBox*boxstatic_castQComboBox*(editor);for(inti0;ibox-count();i){if(strbox-itemText(i)){box-setCurrentIndex(i);}}}voidmyDelegate::setModelData(QWidget*editor,QAbstractItemModel*model,constQModelIndexindex)const{QComboBox*boxstatic_castQComboBox*(editor);model-setData(index,QVariant(box-currentText()),Qt::EditRole);}voidmyDelegate::updateEditorGeometry(QWidget*editor,constQStyleOptionViewItemoption,constQModelIndexindex)const{Q_UNUSED(index);editor-setGeometry(option.rect);}mydelegate.h#ifndefMYDELEGATE_H#defineMYDELEGATE_H#includeQObject#includeQStyledItemDelegate#includeQComboBoxclassmyDelegate:publicQStyledItemDelegate{Q_OBJECTpublic:explicitmyDelegate(QObject*parentnullptr);QWidget*createEditor(QWidget*parent,constQStyleOptionViewItemoption,constQModelIndexindex)constoverride;voidsetEditorData(QWidget*editor,constQModelIndexindex)constoverride;voidsetModelData(QWidget*editor,QAbstractItemModel*model,constQModelIndexindex)constoverride;voidupdateEditorGeometry(QWidget*editor,constQStyleOptionViewItemoption,constQModelIndexindex)constoverride;signals:publicslots:};#endif// MYDELEGATE_H1.在重写 createEditor 函数时一定要指定combox的父亲如果没有指定父亲那么combox将会以一个独立的窗口打开。2. createEditor函数的理解。 原本的qstanditem你想用什么控件去代替它如果保持原样那么就返回null3. createEditor函数的理解。当你双击或者使用其它方式时既想要编辑这个item时。默认它应该显示什么数据4. setModelData函数的理解。当你编辑完这个item时想要修改对应model的数据为什么。5. 自定义输入效果时不能将item设置为不能编辑的状态。