Qt自定义委托实现定制输入的效果
Qt自定义委托实现定制输入的效果
参考博客link 。以下是根据这篇博客修改而来。并写上了自己在编写过程中觉得应该注意的地方。
widget.cpp
#include"widget.h"#include"ui_widget.h"Widget::Widget(QWidget*parent):QWidget(parent),ui(newUi::Widget){ui->setupUi(this);QVBoxLayout*vbox=newQVBoxLayout;QTreeView*treeView=newQTreeView;myDelegate*delegate=newmyDelegate;QStandardItemModel*model=newQStandardItemModel;//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(inti=0;i<5;i++){QStandardItem*item=newQStandardItem(QStringLiteral("%1年级").arg(i+1));model->appendRow(item);for(intj=0;j<5;j++){QStandardItem*item1_0=newQStandardItem(QStringLiteral("%1班").arg(j+1));QStandardItem*item1_1=newQStandardItem(QStringLiteral("Tom"));QStandardItem*item1_2=newQStandardItem(QStringLiteral("100"));QStandardItem*item1_3=newQStandardItem(QStringLiteral("差"));item->appendRow(QList<QStandardItem*>()<<item1_0<<item1_1<<item1_2<<item1_3);}}treeView->setModel(model);treeView->setItemDelegate(delegate);vbox->addWidget(treeView);setLayout(vbox);}Widget::~Widget(){deleteui;}widget.h
#ifndefWIDGET_H#defineWIDGET_H#include<QWidget>#include<QVBoxLayout>#include<QTreeView>#include<QStandardItemModel>#include"mydelegate.h"namespaceUi{classWidget;}classWidget:publicQWidget{Q_OBJECTpublic:explicitWidget(QWidget*parent=nullptr);~Widget();private:Ui::Widget*ui;};#endif// WIDGET_Hmydelegate.cpp
#include"mydelegate.h"myDelegate::myDelegate(QObject*parent):QStyledItemDelegate(parent){}QWidget*myDelegate::createEditor(QWidget*parent,constQStyleOptionViewItem&option,constQModelIndex&index)const{Q_UNUSED(option);//只有第三列的item才使用qcombox进行编辑if(index.column()==3){QComboBox*box=newQComboBox(parent);box->addItems(QStringList()<<QStringLiteral("优")<<QStringLiteral("良")<<QStringLiteral("差"));returnbox;}returnnullptr;}voidmyDelegate::setEditorData(QWidget*editor,constQModelIndex&index)const{QString str=index.data(Qt::EditRole).toString();QComboBox*box=static_cast<QComboBox*>(editor);for(inti=0;i<box->count();i++){if(str==box->itemText(i)){box->setCurrentIndex(i);}}}voidmyDelegate::setModelData(QWidget*editor,QAbstractItemModel*model,constQModelIndex&index)const{QComboBox*box=static_cast<QComboBox*>(editor);model->setData(index,QVariant(box->currentText()),Qt::EditRole);}voidmyDelegate::updateEditorGeometry(QWidget*editor,constQStyleOptionViewItem&option,constQModelIndex&index)const{Q_UNUSED(index);editor->setGeometry(option.rect);}mydelegate.h
#ifndefMYDELEGATE_H#defineMYDELEGATE_H#include<QObject>#include<QStyledItemDelegate>#include<QComboBox>classmyDelegate:publicQStyledItemDelegate{Q_OBJECTpublic:explicitmyDelegate(QObject*parent=nullptr);QWidget*createEditor(QWidget*parent,constQStyleOptionViewItem&option,constQModelIndex&index)constoverride;voidsetEditorData(QWidget*editor,constQModelIndex&index)constoverride;voidsetModelData(QWidget*editor,QAbstractItemModel*model,constQModelIndex&index)constoverride;voidupdateEditorGeometry(QWidget*editor,constQStyleOptionViewItem&option,constQModelIndex&index)constoverride;signals:publicslots:};#endif// MYDELEGATE_H1.在重写 createEditor 函数时一定要指定combox的父亲,如果没有指定父亲那么combox将会以一个独立的窗口打开。
2. createEditor函数的理解。 原本的qstanditem你想用什么控件去代替它,如果保持原样那么就返回null
3. createEditor函数的理解。当你双击或者使用其它方式时(既想要编辑这个item时)。默认它应该显示什么数据
4. setModelData函数的理解。当你编辑完这个item时,想要修改对应model的数据为什么。
5. 自定义输入效果时不能,将item设置为不能编辑的状态。