feat: QStandardItemModel and QStyleItemDelegate
This commit is contained in:
parent
d3349c16b3
commit
77d9584899
42
QtQStandardItemModelEx/ExDelegate.cpp
Normal file
42
QtQStandardItemModelEx/ExDelegate.cpp
Normal 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("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<QComboBox *>(editor);
|
||||||
|
comboBox->addItem(str);
|
||||||
|
//其他组件的话, 此处可以设定写的值一类,eg:setText(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); //设置组件的大小
|
||||||
|
}
|
20
QtQStandardItemModelEx/ExDelegate.h
Normal file
20
QtQStandardItemModelEx/ExDelegate.h
Normal 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
|
@ -61,10 +61,18 @@ void ExQStandardItemModel::init(QStringList &list)
|
|||||||
tempList = aLineText.split(QRegExp("\\s+"), QString::SkipEmptyParts);//正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
|
tempList = aLineText.split(QRegExp("\\s+"), QString::SkipEmptyParts);//正则表达式中\s匹配任何空白字符,包括空格、制表符、换页符等等, 等价于[ \f\n\r\t\v]
|
||||||
|
|
||||||
for ( j = 0; j < COLUMN - 1; j++) { //设置前5列的item
|
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));
|
item = new QStandardItem(tempList.at(j));
|
||||||
m_model->setItem(i - 1, j, item);
|
m_model->setItem(i - 1, j, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
item = new QStandardItem(tempList.at(j)); //最后一列的item
|
item = new QStandardItem(tempList.at(j)); //最后一列的item
|
||||||
item->setCheckable(true); //设置有检查框
|
item->setCheckable(true); //设置有检查框
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
#include <QItemSelectionModel>
|
#include <QItemSelectionModel>
|
||||||
|
|
||||||
|
#include "ExDelegate.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class ExQStandardItemModel;
|
class ExQStandardItemModel;
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,12 @@ CONFIG += c++11
|
|||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
ExQStandardItemModel.cpp
|
ExQStandardItemModel.cpp \
|
||||||
|
ExDelegate.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
ExQStandardItemModel.h
|
ExQStandardItemModel.h \
|
||||||
|
ExDelegate.h
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
ExQStandardItemModel.ui
|
ExQStandardItemModel.ui
|
||||||
|
Loading…
Reference in New Issue
Block a user