#include "DataStruct.h"
#include <QDateTime>
#include <QLineEdit>
#include <QComboBox>
#include <QDateEdit>
CPersonInfoModel::CPersonInfoModel(const QVector<SPersonInfo>& Infos, QObject *parent)
:QAbstractTableModel(parent), m_vecPersonInfos(Infos)
{
}
CPersonInfoModel::~CPersonInfoModel()
{
}
int CPersonInfoModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return m_vecPersonInfos.count();
}
int CPersonInfoModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return 5;
}
QVariant CPersonInfoModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
if (!index.isValid())
{
return QVariant();
}
if (index.row() < 0 || index.row() >= m_vecPersonInfos.count())
{
return QVariant();
}
if (index.row() < 0 || index.column() >= 5)
{
return QVariant();
}
switch (index.column())
{
case 0:
{
if (role == Qt::TextAlignmentRole)
{
return Qt::AlignVCenter | Qt::AlignLeft;
}
else if (role == Qt::DisplayRole)
{
return m_vecPersonInfos[index.row()].strName;
}
else
{
return QVariant();
}
}
case 1:
{
if (role == Qt::DisplayRole)
{
/*if (m_vecPersonInfos[index.row()].eSex == Sex::e_Male)
{
return QStringLiteral("男");
}
else
{
return QStringLiteral("女");
}*/
return m_vecPersonInfos[index.row()].bSex ? QStringLiteral("女") : QStringLiteral("男");
}
else if (role == Qt::TextAlignmentRole)
{
return Qt::AlignVCenter | Qt::AlignHCenter;
}
else
{
return QVariant();
}
}
case 2:
{
if (role == Qt::TextAlignmentRole)
{
return Qt::AlignVCenter | Qt::AlignLeft;
}
else if ( role == Qt::DisplayRole )
{
return m_vecPersonInfos[index.row()].strPhone;
}
}
case 3:
{
if (role == Qt::TextAlignmentRole)
{
return Qt::AlignVCenter | Qt::AlignLeft;
}
else if (role == Qt::DisplayRole)
{
return m_vecPersonInfos[index.row()].strAddr;
}
}
case 4:
{
if (role == Qt::TextAlignmentRole)
{
return Qt::AlignVCenter | Qt::AlignLeft;
}
else if (role == Qt::DisplayRole)
{
return m_vecPersonInfos[index.row()].strBirth;
}
}
default:
return QVariant();
}
}
QVariant CPersonInfoModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
if(role!=Qt::DisplayRole)return QVariant();
if(orientation==Qt::Vertical)return QAbstractItemModel::headerData(section,orientation,role);
switch(section){
case 0:return QStringLiteral("名字");
case 1:return QStringLiteral("性别");
case 2:return QStringLiteral("电话");
case 3:return QStringLiteral("地址");
case 4:return QStringLiteral("生日");
default:return QVariant();
}
}
bool CPersonInfoModel::setData(const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole*/)
{
if ( !index.isValid())
return true;
switch(index.column())
{
case 0:
m_vecPersonInfos[index.row()].strName = value.toString();
return true;
case 2:
m_vecPersonInfos[index.row()].strPhone = value.toString();
return true;
case 3:
m_vecPersonInfos[index.row()].strAddr = value.toString();
return true;
case 1:
m_vecPersonInfos[index.row()].bSex = value.toBool();
return true;
case 4:
m_vecPersonInfos[index.row()].strBirth = value.toString();
}
return true;
}
Qt::ItemFlags CPersonInfoModel::flags(const QModelIndex & index) const
{
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}
void CPersonInfoModel::updateData(QVector<SPersonInfo> new_vec)
{
m_vecPersonInfos = new_vec;
beginResetModel();
endResetModel();
}
CPersonInfoDelegate::CPersonInfoDelegate(QObject* parent /*= nullptr*/)
:QItemDelegate(parent)
{
}
CPersonInfoDelegate::~CPersonInfoDelegate()
{
}
QWidget* CPersonInfoDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QLineEdit *pLineName = new QLineEdit(parent);
QLineEdit *pLinePhone = new QLineEdit(parent);
QLineEdit *pLineAddr = new QLineEdit(parent);
QDateEdit *pDateBirth = new QDateEdit(parent);
QComboBox *pComboSex = new QComboBox(parent);
QRegExp regx("^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$");
QValidator *validator = new QRegExpValidator(regx, pLinePhone );
switch (index.column())
{
case 0:
return pLineName;
break;
case 1:
pComboSex->addItem(QIcon("..\\image\\group_manager.png"), QStringLiteral("男"));
pComboSex->addItem(QIcon("..\\image\\group_creator.png"), QStringLiteral("女"));
return pComboSex;
break;
case 2:
pLinePhone->setValidator(validator);
return pLinePhone;
break;
case 3:
return pLineAddr;
break;
case 4:
pDateBirth->setDate(QDate(1992, 10, 7));
pDateBirth->setCalendarPopup(true);
return pDateBirth;
break;
}
}
void CPersonInfoDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
return QItemDelegate::setEditorData(editor, index);
}
void CPersonInfoDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
/*
if (index.column() == 0)
{
QLineEdit* pEditor = dynamic_cast<QLineEdit*>(editor);
model->setData(index, pEditor->text());
}*/
return QItemDelegate::setModelData(editor, model, index);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ModelViewText.rar (11个子文件)
ModelViewText
DataStruct.cpp 6KB
AddInfoForm.cpp 4KB
tongxunbook.h 700B
mainwindow.cpp 138B
main.cpp 193B
ModelViewText.pro.user 18KB
mainwindow.h 227B
ModelViewText.pro 487B
tongxunbook.cpp 2KB
AddInfoForm.h 613B
DataStruct.h 2KB
共 11 条
- 1
资源评论
- socket_cpp2019-05-13列子是不错,就是不能直接编译,还得自己修改
- chouqiji2018-01-09谢谢楼主学习了
小小哇牛
- 粉丝: 114
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功