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

Qt QtableWidget、QtableView表格删除选中行、删除单行、删除多行

文章目录

  • Qt QtableWidget表格删除选中行
    • 只能选择一行,点击按钮后,删除一行
    • 可以选择中多行,点击按钮后,删除多行
    • 选中某一列中的不同行,点击按钮后,删除多行
  • QTableWidgetSelectionRange介绍
  • QTableWidget的选择模式

Qt QtableWidget表格删除选中行

只能选择一行,点击按钮后,删除一行

设置

	QTableWidget *tb = ui->tableWidget;tb->setSelectionBehavior(QAbstractItemView::SelectRows);tb->setSelectionMode(QAbstractItemView::SingleSelection);

操作

    QTableWidget *tb = ui->tableWidget;int curRow=tb->currentRow();     //当前行号tb->removeRow(curRow);           //删除当前行及其items

可以选择中多行,点击按钮后,删除多行

设置

    QTableWidget *tb = ui->tableWidget;tb->setSelectionBehavior(QAbstractItemView::SelectRows);tb->setSelectionMode(QAbstractItemView::ExtendedSelection);

操作

	QTableWidget *tb = ui->tableWidget;QItemSelectionModel  *m_selection = tb->selectionModel();QModelIndexList indexList = m_selection->selectedIndexes();QList<int> list;int prev =-1,curr=-1;if(indexList.length()>1){//预处理第一行prev  = indexList.at(0).row();list.append(prev);for(int i=1;i<indexList.length();i++){//qDebug() <<"row: "<< indexList.at(i).row() <<"column: "<<indexList.at(i).column();curr = indexList.at(i).row();if(prev ==curr){continue;}else{prev = curr;list.append(prev);}}}else if(indexList.length()==1){list.append(indexList.at(0).row());}else{return;}//从大到小排序,必须从最后一行  往前删除  不然会打乱顺序std::sort(list.rbegin(),list.rend());//根据填充到的数据 删除选中列for(int j = 0; j <list.size(); j++){int cc = list.at(j);tb->removeRow(cc);           //删除当前行及其items}

选中某一列中的不同行,点击按钮后,删除多行

无需设置setSelectionBehavior(QAbstractItemView::SelectRows),但是可以选择的那一列最好设置为不可编辑。按下Ctrl键,选择多行。

设置1

    tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);//选择模式m_selection = new QItemSelectionModel(m_model,this);           //创建选择模型tableView->setSelectionModel(m_selection); //设置选择模型

设置2

        QStandardItem *item1 = new QStandardItem("aa1");item1->setEditable(false);QStandardItem *item2 = new QStandardItem("aa2");item2->setEditable(false);        QList<QStandardItem *> list = {item1,item2};model->appendRow(list);        

操作

            QModelIndexList indexList = m_selection->selectedIndexes();QList<int> list;int sameColumn=-1;for(int i=0;i<indexList.length();i++){if(i==0){sameColumn=indexList.at(i).column();}//qDebug() <<"row: "<< indexList.at(i).row() <<"column: "<<indexList.at(i).column();if(sameColumn!=indexList.at(i).column()){//上面currentColumnChanged信号其实已经让用户只能选择同一列,这里双重保险qDebug()<<"你的选择有不同列,请务必选择同一列的不同行来进行多行删除操作";return;}list.append(indexList.at(i).row());}//从大到小排序,必须从最后一行  往前删除  不然会打乱顺序std::sort(list.rbegin(),list.rend());//根据填充到的数据 删除选中列for(int j = 0; j <list.size(); j++){int cc = list.at(j);m_model->removeRow(cc);}

QTableWidgetSelectionRange介绍

QTableWidgetSelectionRange是Qt框架中用于表示QTableWidget中选定的一块单元格区域的类。以下是如何使用QTableWidgetSelectionRange的一些常见操作:

  1. 获取当前选定的单元格区域:
QList<QTableWidgetSelectionRange> ranges = tableWidget->selectedRanges();
  1. 获取第一个选定的单元格区域的起始行、起始列、行数和列数:
if (!ranges.isEmpty()) {int startRow = ranges.first().topRow();int startColumn = ranges.first().leftColumn();int rowCount = ranges.first().rowCount();int columnCount = ranges.first().columnCount();
}
  1. 遍历所有选定的单元格区域:
foreach (const QTableWidgetSelectionRange &range, ranges) {int startRow = range.topRow();int startColumn = range.leftColumn();int rowCount = range.rowCount();int columnCount = range.columnCount();// 进行处理...
}
  1. 检查特定的单元格区域是否被选定:
QTableWidgetSelectionRange range(1, 1, 3, 3); // 定义一个起始行为1,起始列为1,行数和列数为3的区域
if (tableWidget->selectedRanges().contains(range)) {// 区域被选定
}
  1. 清除所有选定的单元格区域:
tableWidget->clearSelection();

QTableWidgetSelectionRange类提供了一种方便的方式来处理QTableWidget中的选择区域。使用它可以获取和操作选定的单元格区域,进行相关的处理和操作。

QTableWidget的选择模式

QTableWidget通过setSelectionMode()SelectionBehavior来设置选择模式

enum QAbstractItemView::SelectionBehavior

ConstantValueDescription
QAbstractItemView::SelectItems0Selecting single items.
QAbstractItemView::SelectRows1Selecting only rows.
QAbstractItemView::SelectColumns2Selecting only columns.

enum QAbstractItemView::SelectionMode

此枚举指示视图如何响应用户选择:

ConstantValueDescription
QAbstractItemView::SingleSelection1When the user selects an item, any already-selected item becomes unselected. It is possible for the user to deselect the selected item by pressing the Ctrl key when clicking the selected item.
QAbstractItemView::ContiguousSelection4When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
QAbstractItemView::ExtendedSelection3When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
QAbstractItemView::MultiSelection2When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
QAbstractItemView::NoSelection0Items cannot be selected.

相关文章:

Qt QtableWidget、QtableView表格删除选中行、删除单行、删除多行

文章目录 Qt QtableWidget表格删除选中行只能选择一行&#xff0c;点击按钮后&#xff0c;删除一行可以选择中多行&#xff0c;点击按钮后&#xff0c;删除多行选中某一列中的不同行&#xff0c;点击按钮后&#xff0c;删除多行 QTableWidgetSelectionRange介绍QTableWidget的选…...

【代码随想录day24】不同的二叉搜索树

题目 给你一个整数 n &#xff0c;求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种&#xff1f;返回满足题意的二叉搜索树的种数。 示例 1&#xff1a; 输入&#xff1a;n 3 输出&#xff1a;5示例 2&#xff1a; 输入&#xff1a;n 1 输出&#xf…...

数学建模--Subplot绘图的Python实现

目录 1.Subplot函数简介 2.Subplot绘图范例1:绘制规则子图 3.Subplot绘图范例2:绘制不规则子图 4.Subplot绘图范例3:gridspec辅助实战1 5.Subplot绘图范例4:gridspec辅助实战2 1.Subplot函数简介 """ 最近在数学建模种需要绘制多张子图,发现对于subplot函…...

JMeter(三十九):selenium怪异的UI自动化测试组合

文章目录 一、背景二、JMeter+selenium使用过程三、总结一、背景 题主多年前在某社区看到有人使用jmeter+selenium做UI自动化测试的时候,感觉很是诧异、怪异,为啥?众所周知在python/java+selenium+testng/pytest这样的组合框架下,为啥要选择jmeter这个东西[本身定位是接口测…...

c++ 移动构造方法为什么要加noexcept

背景: 最近看了候捷老师的c的教程, 他说移动构造方法要加noexcept, 在vector扩容的时候, 如果有移动构造方法没有加noexcept,是不会调用的. 个人感觉有些神奇, 这就去查下一探究竟. 过程: 测试代码如下: #include <iostream> #include <vector> struct A {A(){s…...

鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统

工程项目管理软件&#xff08;工程项目管理系统&#xff09;对建设工程项目管理组织建设、项目策划决策、规划设计、施工建设到竣工交付、总结评估、运维运营&#xff0c;全过程、全方位的对项目进行综合管理 工程项目各模块及其功能点清单 一、系统管理 1、数据字典&am…...

手把手教你搭建园林园艺小程序商城

现如今&#xff0c;随着互联网的快速发展&#xff0c;小程序成为了企业和个人展示产品和服务的新方式。在园林园艺行业&#xff0c;构建一个园林园艺小程序能够更好地推广和销售自己的产品和服务。那么&#xff0c;如何构建一个园林园艺小程序呢&#xff1f;下面我们来详细介绍…...

Java Iterator(迭代器)

Java迭代器&#xff08;Iterator&#xff09;是 Java 集合框架中的一种机制&#xff0c;是一种用于遍历集合&#xff08;如列表、集合和映射等&#xff09;的接口。 它提供了一种统一的方式来访问集合中的元素&#xff0c;而不需要了解底层集合的具体实现细节。 Iterator 是 …...

Logstash同步MySQL数据到ElasticSearch

当MySQL数据到一定的数量级&#xff0c;而且索引不能实现时&#xff0c;查询就会变得非常缓慢&#xff0c;所以使用ElasticSearch来查询数据。本篇博客介绍使用Logstash同步MySQL数据到ElasticSearch&#xff0c;再进行查询。 测试环境 Windows系统MySQL 5.7Logstash 7.0.1El…...

【C++】运算符重载的示例实现和应用

C运算符重载的格式&#xff1a; operator 运算符 比如要重载 ! 运算符 &#xff1a; operator ! 下面是一个例子&#xff1a; class DemoText{DemoText(string str, int num){m_text str; m_number num;}string m_text;int m_number; }这里来定义两个对象&#xff1a;…...

Kubernetes禁止调度

在Kubernetes中&#xff0c;您可以通过几种方式来禁止某个Pod调度到节点上。以下是一些方法&#xff1a; Node Selector&#xff1a;您可以使用Node Selector来限制Pod只能调度到带有特定标签的节点上。如果您希望完全禁止Pod调度到某些节点上&#xff0c;可以确保这些节点不拥…...

CocosCreator3.8研究笔记(七)CocosCreator 节点和组件的介绍

相信很多新手朋友&#xff0c;肯定会问&#xff0c;CocosCreator 中什么是节点&#xff1f;什么是组件&#xff1f; 一、什么是组件&#xff08;Component&#xff09;&#xff1f; Cocos Creator 3.8 的工作流程是以组件式开发为核心&#xff0c;即以组合而非继承的方式进行游…...

Ceph入门到精通-C++入门知识点

C中的双冒号(::)是作用域分解运算符&#xff08;scope resolution operator&#xff09;。 它主要有以下两种用法&#xff1a; 用于区分同名的不同成员&#xff0c;例如在不同类中声明了同名的成员函数或成员变量&#xff0c;可以使用A::B的方式来特指A类的B成员。当全局变量…...

Ansible之playbook详解和应用实例

目录 一、playbook简介 1.什么是playbook 2.playbook组成 二、应用实例 1.使用playbook安装启用httpd服务 2.使用playbook安装启用nginx服务 三、ansible-playbook其他用法 1.检查yaml文件的语法是否正确 2.检查tasks任务 3.检查指定的主机 4.指定从某个task开始运行…...

经验萃取方法

【经验萃取】 经验萃取不是简单的总结提炼归纳&#xff01; 经验萃取需经过还原、复盘分析、萃取重构 一.经验萃取前三个准备 1.定主题&#xff1a; 萃取主题选择&#xff08;阐述原因、确定级别、差距/问题是源头&#xff09;->多维评分&#xff1a;普遍性、重要性、迫切…...

手写apply方法

<script>/** 手写apply方法 * */Function.prototype.myApply function (context, args) {console.log(this, sss)//fnconst key Symbol()context[key] thiscontext[key](...args)delete context[key]return context[key]}const obj {name: zs,age: 18}function fn …...

Jenkins实现基础CD操作

操作截图 在Jenkins里面设置通过标签进行构建 在Jenkins中进入项目&#xff0c;配置以下 将execute shell换到invoke top-level maven targets之前 在gitlab中配置标签 代码迭代新的版本 项目代码迭代 修改docker-compose.yml 提交新版本的代码 在Jenkins中追加新…...

开源软件合集(Docker)

Docker安装 1.安装命令&#xff1a;curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun2.启动&#xff1a;systemctl start docker3.停止&#xff1a;systemctl stop docker4.重启&#xff1a;systemctl restart docker5.开机启动&#xff1a;systemctl enab…...

Ceph入门到精通-生产日志级别设置

Ceph 子系统及其日志记录级别的信息。 了解 Ceph 子系统及其日志记录级别 Ceph 由多个子系统组成&#xff1a; 每个子系统都有其日志记录级别&#xff1a; 默认情况下存储在 /var/log/ceph/ 目录中的输出日志&#xff08;日志级别&#xff09;存储在内存缓存中的日志&#…...

16-MyCat

一 Mycat概述 1 什么是Mycat 什么是Mycat Mycat是数据库中间件&#xff0c;所谓数据库中间件是连接Java应用程序和数据库中间的软件。 为什么要用Mycat 遇到问题&#xff1a; Java与数据库的紧耦合高访问量高并发对数据库的压力读写请求数据不一致 2 Mycat与其他中间件区别 目…...

终极指南:如何简单快速重置JetBrains IDE试用期

终极指南&#xff1a;如何简单快速重置JetBrains IDE试用期 【免费下载链接】ide-eval-resetter 项目地址: https://gitcode.com/gh_mirrors/id/ide-eval-resetter 你是否曾经在JetBrains IDE的30天试用期结束后&#xff0c;面对灰色的高级功能感到束手无策&#xff1f…...

深度学习中的图像增强技术与TensorFlow实践

1. 图像增强在深度学习中的重要性在解决与图像相关的机器学习问题时&#xff0c;仅仅收集足够的训练图像是不够的。图像增强技术通过创建图像的多样化变体&#xff0c;能够显著提升模型的泛化能力。这对于复杂的物体识别问题尤为重要&#xff0c;因为真实世界中的图像会存在各种…...

【C# 14 原生 AOT 安全部署黄金标准】:Dify 客户端零信任交付的 7 大不可绕过实践

第一章&#xff1a;C# 14 原生 AOT 安全部署 Dify 客户端的战略定位与威胁建模战略定位&#xff1a;轻量、可信、边缘就绪的 AI 交互终端 C# 14 原生 AOT 编译能力使 Dify 客户端摆脱运行时依赖&#xff0c;生成单一、无托管堆、无 JIT 的可执行文件。该模式显著降低攻击面&…...

Blazor组件库选型生死局:MudBlazor vs AntDesign Blazor vs 新晋冠军FluentUI Blazor(2026 Q1真实项目压测对比)

第一章&#xff1a;Blazor组件库选型生死局&#xff1a;MudBlazor vs AntDesign Blazor vs 新晋冠军FluentUI Blazor&#xff08;2026 Q1真实项目压测对比&#xff09;在2026年Q1交付的中大型企业级Blazor WebAssembly应用中&#xff0c;我们对三款主流组件库进行了全链路压测—…...

基于CYBER-VISION零号协议构建跨平台(Ubuntu/Windows)AI应用部署方案

基于CYBER-VISION零号协议构建跨平台&#xff08;Ubuntu/Windows&#xff09;AI应用部署方案 最近在折腾一个挺有意思的AI项目&#xff0c;需要把模型部署到不同的机器上&#xff0c;有的跑Ubuntu&#xff0c;有的跑Windows。一开始觉得&#xff0c;不就是装个环境、跑个服务嘛…...

终极指南:如何用League Director免费制作专业级《英雄联盟》录像

终极指南&#xff1a;如何用League Director免费制作专业级《英雄联盟》录像 【免费下载链接】leaguedirector League Director is a tool for staging and recording videos from League of Legends replays 项目地址: https://gitcode.com/gh_mirrors/le/leaguedirector …...

蔚蓝档案自动化脚本:5步实现游戏日常任务全自动,解放双手专注策略

蔚蓝档案自动化脚本&#xff1a;5步实现游戏日常任务全自动&#xff0c;解放双手专注策略 【免费下载链接】blue_archive_auto_script 支持按轴凹总力战, 无缝制造三解, 用于实现蔚蓝档案自动化的程序( Steam已适配 ) 项目地址: https://gitcode.com/gh_mirrors/bl/blue_arch…...

ROS 摄像头标定实战:从单目到Kinect的完整流程与参数优化

1. 为什么需要摄像头标定&#xff1f; 摄像头标定是机器人视觉开发中不可或缺的一环。想象一下&#xff0c;你戴着一副度数不合适的眼镜看世界&#xff0c;所有物体都会变形扭曲。摄像头也是如此&#xff0c;由于镜头畸变、装配误差等因素&#xff0c;原始图像会产生桶形畸变或…...

前端无障碍访问实现

前端无障碍访问实现&#xff1a;让互联网更包容 在数字化时代&#xff0c;互联网已成为人们获取信息、交流互动的重要渠道。对于残障人士来说&#xff0c;许多网站和应用程序的设计却无形中设置了障碍。前端无障碍访问&#xff08;Web Accessibility&#xff09;的实现&#x…...

模型持久化本身不会提升准确率:揭秘训练集复用导致的“虚假精度”陷阱

模型持久化&#xff08;如使用 joblib 保存 decisiontreeclassifier&#xff09;仅用于部署和复用&#xff0c;不改变模型性能&#xff1b;所谓“准确率从57%升至92%”实为误用——第三次运行时用训练数据直接预测&#xff0c;导致严重过拟合评估&#xff0c;结果完全不可信。 …...