feat: QStandardItemModel and QStyleItemDelegate

This commit is contained in:
touwoyimuli 2019-10-01 11:47:23 +08:00
parent d3349c16b3
commit 77d9584899
5 changed files with 76 additions and 2 deletions

View File

@ -0,0 +1,42 @@
#include "ExDelegate.h"
#include <QComboBox>
ExDelegate::ExDelegate()
{
}
//创建用于编辑的模型数据的widget组件如一个QComboBox组件
QWidget *ExDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *comboBox = new QComboBox(parent);
comboBox->addItem("githubhttps://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<QComboBox *>(editor);
comboBox->addItem(str);
//其他组件的话, 此处可以设定写的值一类egsetText(str) setValue()
}
//将widget上的数据更新到数据模型
void ExDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox *>(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); //设置组件的大小
}

View File

@ -0,0 +1,20 @@
#ifndef EXDELEGATE_H
#define EXDELEGATE_H
#include <QStyledItemDelegate>
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

View File

@ -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); //设置有检查框

View File

@ -6,6 +6,8 @@
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "ExDelegate.h"
namespace Ui {
class ExQStandardItemModel;
}

View File

@ -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