当前位置: 首页 > news >正文

QStringListModel 绑定到QListView

1.QStringListModel 绑定到listView,从而实现MV模型视图
2.通过QStringListModel的新增、删除、插入、上下移动,listView来展示出来
3.下移动一行,传入curRow+2 的个人理解

布局

.h声明 

private:QStringList m_strList;QStringListModel *m_model;

.cpp 

#include "listmodelviewexample.h"
#include "ui_listmodelviewexample.h"ListModelViewExample::ListModelViewExample(QWidget *parent): QMainWindow(parent), ui(new Ui::ListModelViewExample)
{ui->setupUi(this);m_strList<<"北京"<<"上海"<<"广州"<<"深圳"<<"天津"<<"成都"<<"山东"<<"河南"<<"河北";m_model= new QStringListModel(this);m_model->setStringList(m_strList);ui->listView->setModel(m_model);ui->chkEditable->setChecked(true);ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);
}ListModelViewExample::~ListModelViewExample()
{delete ui;
}void ListModelViewExample::on_btnIniList_clicked()
{m_model->setStringList(m_strList);//重新载入
}void ListModelViewExample::on_btnListClear_clicked()
{m_model->removeRows(0,m_model->rowCount());
}void ListModelViewExample::on_chkEditable_clicked(bool checked)
{if(checked)ui->listView->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::SelectedClicked);elseui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}void ListModelViewExample::on_btnListAppend_clicked()
{m_model->insertRow(m_model->rowCount());QModelIndex index= m_model->index(m_model->rowCount()-1,0);m_model->setData(index,"new Item",Qt::DisplayRole);ui->listView->setCurrentIndex(index);
}void ListModelViewExample::on_btnListInsert_clicked()
{QModelIndex index= ui->listView->currentIndex();m_model->insertRow(index.row());m_model->setData(index,Qt::AlignRight,Qt::TextAlignmentRole);ui->listView->setCurrentIndex(index);
}void ListModelViewExample::on_btnListDelete_clicked()
{QModelIndex index= ui->listView->currentIndex();m_model->removeRow(index.row());
}void ListModelViewExample::on_btnListMoveUp_clicked()
{int curRow = ui->listView->currentIndex().row();QModelIndex index = QModelIndex();/*moveRow这个方法,为什么要-1? 我理解如下,1. 在目标位置curRow-1插入一行 插入的新行的行号为curRow-22. 复制原curRow行到目标位置curRow-23. 删除原curRow行*/m_model->moveRow(index,curRow,index,curRow-1);
}void ListModelViewExample::on_btnListMoveDown_clicked()
{int curRow = ui->listView->currentIndex().row();QModelIndex index = QModelIndex();/*moveRow这个方法,为什么要+2? 我理解如下,1. 在目标位置curRow+2插入一行2. 复制curRow行到目标位置curRow+13. 删除curRow行*/m_model->moveRow(index,curRow,index,curRow+2);
}void ListModelViewExample::on_btnClearText_clicked()
{ui->plainTextEdit->clear();
}void ListModelViewExample::on_btnListImport_clicked()
{QStringList tmpList = m_model->stringList();for(int i=0;i<tmpList.size();i++){ui->plainTextEdit->appendPlainText(tmpList.at(i));}
}void ListModelViewExample::on_btnListSort_clicked(bool checked)
{if(checked)m_model->sort(0,Qt::AscendingOrder);elsem_model->sort(0,Qt::DescendingOrder);
}void ListModelViewExample::on_listView_clicked(const QModelIndex &index)
{QString str1 = QString::asprintf("模型索引行号:row=%d,column=%d;\t",index.row(),index.column());QVariant var = m_model->data(index,Qt::DisplayRole);QString str2 = var.toString();int curRow = ui->listView->currentIndex().row();QString str3 = QString::asprintf(";\tlistView:row=%d",curRow);ui->statusbar->showMessage(str1+ str2+ str3);}

 QStringListModel->moveRow  上移传入curRow-1  下移传入curRow+2  这是为什么?有些别扭

 

inline bool QAbstractItemModel::moveRow(const QModelIndex &sourceParent, int sourceRow,const QModelIndex &destinationParent, int destinationChild)

以下仅为个人理解。

destinationChild:创建了一个新行,该新行需要插入的位置,插入新行后,原行删除。

如下移一行,就需要在curRow+2的前面插入一行,插入的新行的行号为curRow+2,删除原行后变成curRow+1

再如上移一行,需要在curRow-1的前面插入一行,插入的新行号变成了curRow-1,而原curRow-1变成了curRow行号

相关文章:

  • 什么是空气电容器?
  • 前端|Vue|WebSocket 超详细 使用ReconnectingWebSocket实现无缝实时通信
  • 对于复杂的网页布局,如多列布局和网格布局,CSS 有哪些最佳实践和技巧?
  • Zabbix 配置grafana对接
  • Spring框架的学习前言
  • spring-boot-starter-data-redis是否支持reactive响应式编程
  • 【INTEL(ALTERA)】nios ii构建自定义新lib 的编译器标记
  • ANN文献综述
  • docker -run hello-world超时
  • Python正则表达式入门用法(下)
  • STM32自己从零开始实操07:电机电路原理图
  • 数据结构和算法,单链表的实现(kotlin版)
  • 技术赋能教育:校园3D电子地图与AR导航解决方案
  • 【图论】树链剖分
  • 基于docker环境及Harbor部署{很简短一点了,耐心看吧}
  • 网络传输文件的问题
  • 【译】JS基础算法脚本:字符串结尾
  • download使用浅析
  • golang中接口赋值与方法集
  • JS题目及答案整理
  • JS字符串转数字方法总结
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • 简单基于spring的redis配置(单机和集群模式)
  • 聊聊flink的TableFactory
  • 浅谈web中前端模板引擎的使用
  • 时间复杂度与空间复杂度分析
  • 项目管理碎碎念系列之一:干系人管理
  • 用jQuery怎么做到前后端分离
  • 智能合约开发环境搭建及Hello World合约
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • #ubuntu# #git# repository git config --global --add safe.directory
  • $L^p$ 调和函数恒为零
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (规划)24届春招和25届暑假实习路线准备规划
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (十六)一篇文章学会Java的常用API
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • ***详解账号泄露:全球约1亿用户已泄露
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .aanva
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .Net FrameWork总结
  • .net 程序发生了一个不可捕获的异常
  • .net 设置默认首页
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET编程C#线程之旅:十种开启线程的方式以及各自使用场景和优缺点
  • .net反混淆脱壳工具de4dot的使用