开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

cpp_workspace/QtTableViewDelegate

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
MyTableModel.cpp 7.78 KB
一键复制 编辑 原始数据 按行查看 历史
龚建波 提交于 2020年09月18日 23:38 +08:00 . tablemodel sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
#include "MyTableModel.h"
#include <QDateTime>
MyTableModel::MyTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
void MyTableModel::setModelData(const QList<MyModelItem> &datas)
{
//重置model数据之前调用beginResetModel,此时会触发modelAboutToBeReset信号
beginResetModel();
//重置model中的数据
modelData=datas;
//数据设置结束后调用endResetModel,此时会触发modelReset信号
endResetModel();
//注意:reset model后,选中的item会失效,我们可以自己写保存和恢复选中项的逻辑
//如果表的行列数是固定的,只是数据变更了,我们可以用 dataChanged 信号来请求刷新。
//emit dataChanged(index(0,0),index(RowMax-1,ColMax-1),QVector<int>());
}
QList<MyModelItem> MyTableModel::getModelData() const
{
//将内存数据返回
return modelData;
}
bool MyTableModel::insertModelData(int row, const MyModelItem &datas)
{
//row为0就是开始,为rowcount就在尾巴
if(row<0||row>rowCount())
return false;
//需要将操作放到beginInsertRows和endInsertRows两个函数调用之间
beginInsertRows(QModelIndex(), row, row);
//在接口对应行插入空数据
modelData.insert(row,datas);
endInsertRows();
return true;
}
void MyTableModel::setHorHeaderData(const QList<QString> &headers)
{
//自定义的表头设置接口
horHeaderData=headers;
emit headerDataChanged(Qt::Horizontal, 0, headers.count()-1);
}
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
//注意,如果用了sortproxymodel,这个section是实际数据的index,不是界面看到的index
//区分横表头和竖表头
if(orientation == Qt::Horizontal){
//这里我们只设置居中对齐和文本
if (role == Qt::DisplayRole){
//这里把横项列表头的文本设计为可以设置的
if(section>=0 && section<horHeaderData.count())
return horHeaderData.at(section);
return QString("Col %1").arg(section + 1);
}else if(role == Qt::TextAlignmentRole){
return Qt::AlignCenter;
}
}else{
if (role == Qt::DisplayRole)
return QString("Row %1").arg(section + 1);
else if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
}
return QVariant();
}
bool MyTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
//role懒得判断了
role;
//设计为横项列表头可以设置
if (orientation == Qt::Horizontal && section>=0 && section<horHeaderData.count()) {
horHeaderData[section] = value.toString();
emit headerDataChanged(orientation, section, section);
return true;
}
return false;
}
int MyTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
//返回表格行数
return modelData.count();
}
int MyTableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
//返回表格列数
return MyModelItem::columnCount();
}
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole || role == Qt::EditRole)
{
//DisplayRole返回显示的文本值
const int row = index.row();
switch(index.column())
{
case 0: return modelData.at(row).name;
case 1: return modelData.at(row).age;
case 2: return modelData.at(row).info;
}
}
return QVariant();
}
bool MyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
//将界面修改的值进行保存
if (index.isValid() && role == Qt::EditRole) {
const int row = index.row();
switch(index.column())
{
case 0: modelData[row].name = value.toString(); break;
case 1: modelData[row].age = value.toInt(); break;
case 2: modelData[row].info = value.toString(); break;
}
//发送信号触发刷新
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
//单元格允许的操作,至少得是ItemIsEnabled的才能进行其他操作
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
}
bool MyTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
//row为0就是开始,为rowcount就在尾巴
if(row<0||count<1||row>rowCount())
return false;
//需要将操作放到beginInsertRows和endInsertRows两个函数调用之间
beginInsertRows(parent, row, row + count - 1);
for(int i=row;i<row+count;i++)
{
//在接口对应行插入空数据
modelData.insert(i,MyModelItem());
}
endInsertRows();
return true;
}
bool MyTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
if(row<0||count<1||row+count>rowCount())
return false;
//需要将操作放到beginRemoveRows和endRemoveRows两个函数调用之间
beginRemoveRows(parent, row, row + count - 1);
for(int i=row+count-1;i>=row;i--)
{
//移除该行数据
modelData.removeAt(i);
}
endRemoveRows();
return true;
}
void MyTableModel::sort(int column, Qt::SortOrder order)
{
if(modelData.isEmpty()||column<0||column>=columnCount())
return;
//判断升序降序
const bool is_asc = (order == Qt::AscendingOrder);
//排序
std::sort(modelData.begin(), modelData.end(),
[column, is_asc, this](const MyModelItem &left,const MyModelItem &right){
//我用QVariant只是在以前的基础上改的,自定义类型可以不用这个
//这里假设单元格数据都是任意类型的
const QVariant left_val = left.at(column);
const QVariant right_val = right.at(column);
//辅助接口,a<b返回true
return is_asc
?lessThan(left_val,right_val)
:lessThan(right_val,left_val);
});
//更新view
dataChanged(index(0,0),index(modelData.count()-1,columnCount()-1));
}
bool MyTableModel::lessThan(const QVariant &left, const QVariant &right) const
{
//参照QAbstractItemModelPrivate::isVariantLessThan的实现
//这些都是通用型的排序规则,一般我们会有自定义的需求,比如根据字符串中的数字排序
//有些类型需要包含头文件才能使用,如datetime
if (left.userType() == QMetaType::UnknownType)
return false;
if (right.userType() == QMetaType::UnknownType)
return true;
switch (left.userType()) {
case QMetaType::Int:
return left.toInt() < right.toInt();
case QMetaType::UInt:
return left.toUInt() < right.toUInt();
case QMetaType::LongLong:
return left.toLongLong() < right.toLongLong();
case QMetaType::ULongLong:
return left.toULongLong() < right.toULongLong();
case QMetaType::Float:
return left.toFloat() < right.toFloat();
case QMetaType::Double:
return left.toDouble() < right.toDouble();
case QMetaType::QChar:
return left.toChar() < right.toChar();
case QMetaType::QDate:
return left.toDate() < right.toDate();
case QMetaType::QTime:
return left.toTime() < right.toTime();
case QMetaType::QDateTime:
return left.toDateTime() < right.toDateTime();
case QMetaType::QString: break;
default: break;
}
//Locale表示支持本地字符串
//if (isLocaleAware)
return left.toString().localeAwareCompare(right.toString()) < 0;
//else
// return left.toString().compare(right.toString(), cs) < 0;
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cpp_workspace/QtTableViewDelegate.git
git@gitee.com:cpp_workspace/QtTableViewDelegate.git
cpp_workspace
QtTableViewDelegate
QtTableViewDelegate
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /