diff --git a/QtQStandardItemModelEx/ExDelegate.cpp b/QtQStandardItemModelEx/ExDelegate.cpp new file mode 100644 index 0000000..13a55da --- /dev/null +++ b/QtQStandardItemModelEx/ExDelegate.cpp @@ -0,0 +1,42 @@ +#include "ExDelegate.h" +#include + +ExDelegate::ExDelegate() +{ +} + + +//创建用于编辑的模型数据的widget组件,如一个QComboBox组件 +QWidget *ExDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QComboBox *comboBox = new QComboBox(parent); + comboBox->addItem("github:https://github.com/touwoyimuli"); + comboBox->addItem("csdn博客:https://blog.csdn.net/qq_33154343"); + comboBox->addItem("github.io博客:https://touwoyimuli.github.io/"); + comboBox->addItem("https://www.google.com/ qt 学习 加油 2019-09-30"); + + return comboBox; //返回此编辑器 +} + +//从数据模型获取数据,显示在代理组件之中,让其编辑 +void ExDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +{ + QString str = index.model()->data(index, Qt::EditRole).toString(); + QComboBox *comboBox = static_cast(editor); + comboBox->addItem(str); + //其他组件的话, 此处可以设定写的值一类,eg:setText(str) setValue() +} + +//将widget上的数据更新到数据模型 +void ExDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + QComboBox *comboBox = static_cast(editor); + QString str = comboBox->currentText(); + model->setData(index, str, Qt::EditRole); +} + +//用于给widget组件设置一个合适的大小 +void ExDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + editor->setGeometry(option.rect); //设置组件的大小 +} diff --git a/QtQStandardItemModelEx/ExDelegate.h b/QtQStandardItemModelEx/ExDelegate.h new file mode 100644 index 0000000..711e343 --- /dev/null +++ b/QtQStandardItemModelEx/ExDelegate.h @@ -0,0 +1,20 @@ +#ifndef EXDELEGATE_H +#define EXDELEGATE_H + +#include + + +class ExDelegate : public QStyledItemDelegate +{ +public: + ExDelegate(); + +public: + //若是写代理的组件,那么必须要有下面这是个重写这四个函数 + virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; //创建用于编辑的模型数据的widget组件,如一个QComboBox组件 + virtual void setEditorData(QWidget *editor, const QModelIndex &index) const override; //从数据模型获取数据,显示在代理组件editor之中,让其编辑 + virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; //代理组件editor上的数据更新到数据模型 + virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; //用于给widget组件设置一个合适的大小 +}; + +#endif // EXDELEGATE_H diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.cpp b/QtQStandardItemModelEx/ExQStandardItemModel.cpp index 5eb7b7b..2635ec7 100644 --- a/QtQStandardItemModelEx/ExQStandardItemModel.cpp +++ b/QtQStandardItemModelEx/ExQStandardItemModel.cpp @@ -61,10 +61,18 @@ void ExQStandardItemModel::init(QStringList &list) tempList = aLineText.split(QRegExp("\\s+"), QString::SkipEmptyParts);//正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v] for ( j = 0; j < COLUMN - 1; j++) { //设置前5列的item + + if (j == 3) { + ExDelegate *itemDelegate = new ExDelegate(); + ui->tableView->setItemDelegateForColumn(3, itemDelegate); + } + item = new QStandardItem(tempList.at(j)); m_model->setItem(i - 1, j, item); } + + item = new QStandardItem(tempList.at(j)); //最后一列的item item->setCheckable(true); //设置有检查框 diff --git a/QtQStandardItemModelEx/ExQStandardItemModel.h b/QtQStandardItemModelEx/ExQStandardItemModel.h index 2c119e7..4264f91 100644 --- a/QtQStandardItemModelEx/ExQStandardItemModel.h +++ b/QtQStandardItemModelEx/ExQStandardItemModel.h @@ -6,6 +6,8 @@ #include #include +#include "ExDelegate.h" + namespace Ui { class ExQStandardItemModel; } diff --git a/QtQStandardItemModelEx/QtQStandardItemModelEx.pro b/QtQStandardItemModelEx/QtQStandardItemModelEx.pro index f9056fb..43457c5 100644 --- a/QtQStandardItemModelEx/QtQStandardItemModelEx.pro +++ b/QtQStandardItemModelEx/QtQStandardItemModelEx.pro @@ -26,10 +26,12 @@ CONFIG += c++11 SOURCES += \ main.cpp \ - ExQStandardItemModel.cpp + ExQStandardItemModel.cpp \ + ExDelegate.cpp HEADERS += \ - ExQStandardItemModel.h + ExQStandardItemModel.h \ + ExDelegate.h FORMS += \ ExQStandardItemModel.ui