QStringListModel 绑定到QListView

news2024/10/6 10:28:09

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);
    else
        ui->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-2
    2. 复制原curRow行到目标位置curRow-2
    3. 删除原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+1
    3. 删除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);
    else
        m_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行号

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1890701.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Sping源码(九)—— Bean的初始化(非懒加载)—mergeBeanDefinitionPostProcessor

序言 前几篇文章详细介绍了Spring中实例化Bean的各种方式&#xff0c;其中包括采用FactoryBean的方式创建对象、使用反射创建对象、自定义BeanFactoryPostProcessor以及构造器方式创建对象。 创建对象 这里再来简单回顾一下对象的创建&#xff0c;不知道大家有没有这样一个疑…

【C语言】顺序表经典算法

本文介绍的是两道顺序表经典算法题目。 移除元素 &#xff08;来源&#xff1a;LeetCode&#xff09; 题目 分析 我们很容易想到的办法是去申请一个新的数组&#xff0c;遍历原数组不等于val就把它拿到新数组里。但是题目的要求是不使用额外空间&#xff0c;所以这种方法我们…

Vue进阶(四十五)Jest集成指南

文章目录 一、前言二、环境检测三、集成问题汇总四、拓展阅读 一、前言 在前期博文《Vue进阶&#xff08;八十八&#xff09;Jest》中&#xff0c;讲解了Jest基本用法及应用示例。一切顺利的话&#xff0c;按照文档集成应用即可&#xff0c;但是集成过程中遇到的问题可能五花八…

【微机原理及接口技术】中断控制器8259A

【微机原理及接口技术】中断控制器8259A 文章目录 【微机原理及接口技术】中断控制器8259A前言一、介绍二、8259A的内部结构和引脚三、8259A的中断工作过程四、8259A的工作方式五、8259A的编程六、外部中断服务程序总结 前言 本篇文章将就8259芯片展开介绍&#xff0c;8259A的…

Chapter10 高级纹理——Shader入门精要学习笔记

Chapter10 高级纹理 一、立方体纹理1.基本概念①组成②采样 2.天空盒子 Sky Box3.环境映射三种方法①特殊布局的纹理创建②手动创建Cubemap——老方法③脚本生成 4.反射5.折射6.菲涅尔反射 二、渲染1.镜子效果2.玻璃效果3.渲染纹理 vs GrabPass 三、程序纹理1.简单程序纹理2.Un…

mov文件怎么转换成mp4格式?这四种转换方法超级好用!

mov文件怎么转换成mp4格式&#xff1f;在数字娱乐的世界中&#xff0c;你是否曾遇到过MOV格式的视频&#xff1f;也许&#xff0c;对于许多人来说&#xff0c;这并不是一个常见的格式&#xff0c;但这并非偶然&#xff0c;首先&#xff0c;我们来谈谈MOV的兼容性问题&#xff0…

读书笔记-《Spring技术内幕》(三)MVC与Web环境

前面我们学习了 Spring 最核心的 IoC 与 AOP 模块&#xff08;读书笔记-《Spring技术内幕》&#xff08;一&#xff09;IoC容器的实现、读书笔记-《Spring技术内幕》&#xff08;二&#xff09;AOP的实现&#xff09;&#xff0c;接下来继续学习 MVC&#xff0c;其同样也是经典…

Linux动态库的制作

Linux操作系统支持的函数库分为&#xff1a; 静态库&#xff0c;libxxx.a&#xff0c;在编译时就将库编译进可执行程序中。 优点&#xff1a;程序的运行环境中不需要外部的函数库。 缺点&#xff1a;可执行程序大 动态库&#xff0c;又称共享库&#xff0c;libxxx.so&#…

Linux之进程控制(下)

目录 进程替换的概念 进程替换的函数 execl​编辑 execlp execle execv execvp execve 上期&#xff0c;我们学习了进程创建&#xff0c;进程终止和进程等待&#xff0c;今天我们要学习的是进程控制中相对重要的板块------进程替换。 进程替换的概念 在进程创建时&…

中国经济昆虫志(55卷)

中国经济昆虫志&#xff0c;共55卷&#xff0c;内容包括概述、形态特征、分类等。各级分类单元均编有检索表&#xff0c;每个种有特征描述、地理分布&#xff0c;有的还记载有生活习性和防治方法。为便于鉴定&#xff0c;绘制有特征图和彩色图。 包括鞘翅目天牛科、半翅目蝽科、…

C - Tile Distance 2

分析&#xff1a;每穿过一行就会加一 先纵向走&#xff0c;再横向走 统一用砖头的左半部分计算 #include<bits/stdc.h> using namespace std; typedef long long ll; int main(){ ll sx,sy,tx,ty;cin>>sx>>sy>>tx>>ty; if((sxsy)%2!0)…

使用CubeIDE调试项目现stm32 no source available for “main() at 0x800337c:

使用CubeIDE调试项目现stm32 no source available for "main() at 0x800337c&#xff1a; 问题描述 使用CubeIDE编译工程代码和下载都没有任何问题&#xff0c;点击Debug调试工程时&#xff0c;出现stm32 no source available for "main() at 0x800337c 原因分析&a…

【C++】#1

关键字&#xff1a; 基本框架、多个main执行、快捷键、cout规则 基本框架&#xff1a; #include <iostream> using namespace std;int main() {//具体内容return 0; } 多个main函数可执行&#xff1a; 常用快捷键&#xff1a; cout规则&#xff1a;

小米MIX Fold 4折叠屏手机背面渲染图曝光

ChatGPT狂飙160天&#xff0c;世界已经不是之前的样子。 更多资源欢迎关注 7 月 3 日消息&#xff0c;消息源 Evan Blass 今天在 X 平台发布推文&#xff0c;分享了小米 MIX Fold 4 折叠屏手机的高清渲染图&#xff08;图片有加工成分在&#xff0c;最终零售版本可能会存在差异…

Cypress测试:7个快速解决问题的调试技巧!

以下为作者观点&#xff1a; 快速编写代码是一项宝贵的技能&#xff0c;但能够有效调试和解决错误和bug&#xff0c;更是一个软件开发人员具有熟练技能的标志。调试是开发过程中的一个关键环节&#xff0c;可以确保软件按预期运行并满足用户需求。 Cypress 调试简介 Cypress …

可充电纽扣电池ML2032充电电路设计

如图&#xff0c;可充电纽扣电池ML2032充电电路设计。 图中二极管是为了防止电流倒灌&#xff0c; 电阻分压出3.66v&#xff0c;再减掉二极管压降&#xff08;约0.4v)得3.26V&#xff0c;加在电池正负极充电。 随着电池电量的积累&#xff0c;充电电流逐步减小&#xff0c;极限…

Eslint与Prettier搭配使用

目录 前置准备 Eslint配置 Prettier配置 解决冲突 前置准备 首先需要安装对应的插件 然后配置settings.json 点开之后就会进入settings.json文件里&#xff0c;加上这两个配置 // 保存的时候自动格式化 "editor.formatOnSave": true, // 保存的时候使用prettier进…

ctfshow sql注入 web234--web241

web234 $sql "update ctfshow_user set pass {$password} where username {$username};";这里被过滤了&#xff0c;所以我们用\转义使得变为普通字符 $sql "update ctfshow_user set pass \ where username {$username};";那么这里的话 pass\ where…

一文包学会ElasticSearch的大部分应用场合

ElasticSearch 官网下载地址&#xff1a;Download Elasticsearch | Elastic 历史版本下载地址1&#xff1a;Index of elasticsearch-local/7.6.1 历史版本下载地址2&#xff1a;Past Releases of Elastic Stack Software | Elastic ElasticSearch的安装(windows) 安装前所…

深入了解激光粒度分析仪:检测物质粒度分布的利器

在科研、工业生产以及环境监测等多个领域中&#xff0c;精确测量物质粒度分布是确保产品质量、研究准确性和环境安全的重要步骤。 近年来&#xff0c;激光粒度分析仪以其独特的技术优势&#xff0c;在这些领域发挥着越来越重要的作用。 在这篇文章中&#xff0c;佰德将带您了…