【QT入门】 QListWidget各种常见用法详解之列表模式

news2024/10/10 6:22:03

往期回顾

【QT入门】 Qt代码创建布局之setLayout使用-CSDN博客

【QT入门】 Qt代码创建布局之多重布局变换与布局删除技巧-CSDN博客

【QT入门】 QTabWidget各种常见用法详解-CSDN博客

 【QT入门】 QListWidget各种常见用法详解之列表模式

QListWidget有列表和图标两种显示模式,默认是列表模式我们就先看列表模式。

enum ViewMode
{
ListMode, //列表模式
IconMode //图标模式
};

 一样的,学一个新的控件,首先要看他是怎么构造的,其次看怎么用,常用的方法,然后要去看他的信号函数,特别是熟悉常用方法和信号。而且ui界面创建和手动代码创建都要学会。

一、UI界面创建

ui界面创建List Widget是很简单的,找到List Widget拖动到窗口上即可。

在里面可以实现添加,删除选项的基本功能,还能对添加的每个Item进行样式设计,添加图片等,ui操作简单明了,还很容易实现。

二、手动代码创建 

1、最终效果

2、QListWidgetItem 

主要用到的就是 QListWidgetItem 这个类去创建QListWidget里面的选项,也就是Item

我先用ui搭建了整体框架

然后再用代码添加Item,用addItem()方法。

代码很简单,就没什么好说的,方法参数什么的,忘记了就点进去看。

值得一提的是,这里依旧是可以自定义Item对象,因为Item里面实际放的是Widget,所以我们可以创建Widget对象用ui界面设计了放进Item对象里去。最终效果里的头像、昵称、内容就是用的自定义Item对象,搭了一个大概框架,如果做精细化,其实就类似于微信里一个个好友聊天框。

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QListWidgetItem *pListItem1 = new QListWidgetItem(QIcon(":/new/prefix2/res/editor.png"),"editor");
    QListWidgetItem *pListItem2 = new QListWidgetItem(QIcon(":/new/prefix2/res/env.png"),"env");
    QListWidgetItem *pListItem3 = new QListWidgetItem(QIcon(":/new/prefix2/res/help.png"),"help");
    QListWidgetItem *pListItem4 = new QListWidgetItem(QIcon(":/new/prefix2/res/kits.png"),"kits");
    QListWidgetItem *pListItem5 = new QListWidgetItem(QIcon(":/new/prefix2/res/vim.png"),"vim");
    QListWidgetItem *pListItem6 = new QListWidgetItem;

    ui->listWidget->addItem(pListItem1);
    ui->listWidget->addItem(pListItem2);
    ui->listWidget->addItem(pListItem3);
    ui->listWidget->addItem(pListItem4);
    ui->listWidget->addItem(pListItem5);
    ui->listWidget->addItem(pListItem6);

    CuserItem *cuseritem = new CuserItem(this);
    ui->listWidget->setItemWidget(pListItem6,cuseritem);

}

三、附加功能:右键单击菜单,item删除

这里主要是对列表模式的一个扩展,就是说当我们用列表模式能够在listWidget里显示item的时候,我们想要实现单击选项出现菜单,实现删除item等功能。

1、思路

1、先是创建item加入到listWidget里面去,
2、然后是执行初始化菜单函数,在初始化函数的实现里,创建action对象并对每个对象连接相应的槽函数
3、设置菜单策略,不设置无法显示菜单
4、最后是连接item对象和菜单,也就是右键单击item才出现相应的菜单

2、知识点分析

 2.1、设置菜单策略
1、ui.listWidget->setContextMenuPolicy(Qt::CustomContextMenu);

将listWidget对象的上下文菜单策略设置为Qt::CustomContextMenu。这意味着可以为列表部件设置自定义上下文菜单。必须设置,不然无法显示菜单。

2.2、创建指针
2、QListWidgetItem* pItem = ui.listWidget->itemAt(pos);

这行代码创建了一个指向QListWidgetItem对象的指针pItem,该对象对应于listWidget中位置pos处的项目。

要知道这个itemAt方法,是Qt中用于检索给定坐标位置处的列表部件中的项目的方法 

QListWidgetItem* QListWidget::itemAt(const QPoint& position) const;

如果给定的位置处有项目,则返回一个指向该项目的QListWidgetItem指针;如果该位置没有项目,则返回nullptr。这个方法通常用于处理鼠标事件,以确定用户单击或右键单击的是哪个项目。 由于我们要是实现单击选择删除item,所以需要用到这个方法。

2.3、删除item
2.3.1动态删除

知道item的名字, 根据名字删除,这种可用于用户掉线,信息丢失等。

在listWidget中查找包含字符串"kits"的项目,然后将找到的第一个匹配项目移除并删除。

2.3.1.1代码示例
      QList<QListWidgetItem*> list;
      list = ui.listWidget->findItems("kits", Qt::MatchCaseSensitive);
      QListWidgetItem* sel = list[0];
      int r = ui.listWidget->row(sel);
      QListWidgetItem* item = ui.listWidget->takeItem(r); 
      ui.listWidget->removeItemWidget(item);
      delete item; 

我们一一分析这段代码:

QList<QListWidgetItem*> list;声明了一个QList类型的列表list,用于存储QListWidgetItem*指针。
list = ui.listWidget->findItems("kits", Qt::MatchCaseSensitive);调用findItems函数在listWidget中查找内容为"kits"的项目,并以区分大小写的方式进行匹配。搜索结果存储在列表list中。
QListWidgetItem* sel = list[0];从存储搜索结果的列表中获取第一个匹配项,并将其赋值给指针sel。        
int r = ui.listWidget->row(sel);获取项目sel在listWidget中的行索引,并将其存储在变量r中。
QListWidgetItem* item = ui.listWidget->takeItem(r);从listWidget中移除第r行的项目,并将其返回给指针item。此时,该项目已经不再属于listWidget。
ui.listWidget->removeItemWidget(item);        移除与项目item关联的部件。这一步是为了确保在删除项目之前先移除其部件,以避免内存泄漏。
delete item;删除项目item对象,释放其在内存中的空间。
2.3.2右键点击删除
QListWidgetItem* item = ui.listWidget-> currentItem();
ui.listWidget->removeItemWidget(item);
delete item;

这个就没什么好说的,很简单,推荐用这个,就是找到当前点击的item并移除。 

3、完整实例代码

#include "QListWidget_02.h"
#include <QListWidgetItem>
#include <QAction>
#include <QMessageBox>

QListWidget_02::QListWidget_02(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    
    QListWidgetItem* pListItem1 = new QListWidgetItem(QIcon(":/QListWidget_02/res/editor.png"), "editor");
    QListWidgetItem* pListItem2 = new QListWidgetItem(QIcon(":/QListWidget_02/res/env.png"), "env");
    QListWidgetItem* pListItem3 = new QListWidgetItem(QIcon(":/QListWidget_02/res/help.png"), "help");
    QListWidgetItem* pListItem4 = new QListWidgetItem(QIcon(":/QListWidget_02/res/kits.png"), "kits");
    QListWidgetItem* pListItem5 = new QListWidgetItem(QIcon(":/QListWidget_02/res/vim.png"), "vim");

    ui.listWidget->addItem(pListItem1);
    ui.listWidget->addItem(pListItem2);
    ui.listWidget->addItem(pListItem3);
    ui.listWidget->addItem(pListItem4);
    ui.listWidget->addItem(pListItem5);

    initRight();

    //设置菜单策略,必须设置,不然无法显示菜单
    ui.listWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    //连接信号槽
    connect(ui.listWidget, &QListWidget::customContextMenuRequested, 
        this, &QListWidget_02::onPopupRightMenu);

}

QListWidget_02::~QListWidget_02()
{}

void QListWidget_02::onPopupRightMenu(const QPoint& pos)
{
    QListWidgetItem* pItem = ui.listWidget->itemAt(pos);

    if (!pItem)
    {
        return;
    }

    m_pRightMenu->exec(QCursor::pos());
}

void QListWidget_02::initRight()
{
    m_pRightMenu = new QMenu(this);

    QAction* qAce1 = new QAction(u8"动作一", this);
    QAction* qAce2 = new QAction(u8"动作二", this);
    QAction* qAce3 = new QAction(u8"动作三", this);
    QAction* qAce4 = new QAction(u8"删除", this);

    m_pRightMenu->addAction(qAce1);
    m_pRightMenu->addAction(qAce2);
    m_pRightMenu->addAction(qAce3);
    m_pRightMenu->addAction(qAce4);

    connect(qAce1, &QAction::triggered, [=] {
        QMessageBox::information(this, "title", "qAce1");
        });

    connect(qAce4, &QAction::triggered, [=] {
    //方式1:知道item的名字, 根据名字删除,这种可用于用户掉线,信息丢失等,动态删除
      //  QList<QListWidgetItem*> list;
      //list = ui.listWidget->findItems("kits", Qt::MatchCaseSensitive);
      //QListWidgetItem* sel = list[0];
      //int r = ui.listWidget->row(sel);
      //QListWidgetItem* item = ui.listWidget->takeItem(r); ui.listWidget->removeItemWidget(item);
      //delete item; 


    //方式2:
    //    右键点击删除,或者选中删除
            QListWidgetItem* item = ui.listWidget-> currentItem();
        ui.listWidget->removeItemWidget(item);
        delete item;

        });
}

都看到这里了,点个赞再走呗朋友~

加油吧,预祝大家变得更强!

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

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

相关文章

数据结构刷题篇 之 【力扣二叉树基础OJ】详细讲解(含每道题链接及递归图解)

有没有一起拼用银行卡的&#xff0c;取钱的时候我用&#xff0c;存钱的时候你用 1、相同的树 难度等级&#xff1a;⭐ 直达链接&#xff1a;相同的树 2、单值二叉树 难度等级&#xff1a;⭐ 直达链接&#xff1a;单值二叉树 3、对称二叉树 难度等级&#xff1a;⭐⭐ 直达…

Delphi模式编程

文章目录 Delphi模式编程涉及以下几个关键方面&#xff1a;**设计模式的应用****Delphi特性的利用****实际开发中的实践** Delphi模式编程的实例 Delphi模式编程是指在使用Delphi这一集成开发环境&#xff08;IDE&#xff09;和Object Pascal语言进行软件开发时&#xff0c;采用…

vivado 器件编程

生成器件镜像后 &#xff0c; 下一步是将其下载到目标器件。 Vivado IDE 具有内置原生的系统内器件编程功能用于执行此操作。 Vivado Design Suite 和 Vivado Lab Edition 都包含相应的功能 &#xff0c; 支持您连接到包含 1 个或多个 FPGA 或 ACAP 的硬 件&#xff0c; 以…

9、鸿蒙学习-开发及引用静态共享包(API 9)

HAR&#xff08;Harmony Archive&#xff09;是静态共享包&#xff0c;可以包含代码、C库、资源和配置文件。通过HAR可以实现多个模块或多个工程共享ArkUI组件、资源等相关代码。HAR不同于HAP&#xff0c;不能独立安装运行在设备上&#xff0c;只能作为应用模块的依赖项被引用。…

使用Docker Compose一键部署前后端分离项目(图文保姆级教程)

一、安装Docker和docker Compose 1.Docker安装 //下载containerd.io包 yum install https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm //安装依赖项 yum install -y yum-utils device-mapper-persistent-data l…

VTK 9.2.6 源码和VTK Examples 编译 Visual Studio 2022

对于编译 VTK 源码和编译详细的说明&#xff1a; VTK 源码编译&#xff1a; 下载源码&#xff1a; 从 VTK 官方网站或者 GitHub 获取源代码。官网目前最近的9.3.0有问题&#xff0c;见VTK 9.3.0 编译问题 Visual Studio 2022去gitlab上选择9.2.6分支进行clone CMake 配置&…

探索数据结构:链式队与循环队列的模拟、实现与应用

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;数据结构与算法 贝蒂的主页&#xff1a;Betty’s blog 1. 队列的定义 队列&#xff08;queue&#xff09;是一种只允许在一端进…

系统分析师-参考模型

前言 网络术语中的参考模型指的是OSI参考模型&#xff0c;由ISO&#xff08;国际标准化组织&#xff09;制定的一套普遍适用的规范集合&#xff0c;以使得全球范围的计算机平台可进行开放式通信。 ISO创建了一个有助于开发和理解计算机的通信模型&#xff0c;即开放系统互联OS…

openwrt 编译mysql数据库固件,并调用

前言 openwrt 编译源码mysql数据库&#xff0c;并编写demo调用 一、整体架构设计 作者要做一个项目&#xff0c;没有后端服务&#xff0c;只有一个电脑&#xff0c;需要在电脑上安装mysql服务端。然后在设备上安装mysql客户端。 二、PC安装mysql 1.官网链接 自行百度安装&a…

一文彻底搞懂spring循环依赖

文章目录 1. 什么是循环依赖2. Spring怎么解决循环依赖3. 无法处理的循环依赖 1. 什么是循环依赖 Spring 中的循环依赖是指两个或多个 Bean 之间相互依赖&#xff0c;形成一个循环引用的情况。在 Spring 容器中&#xff0c;循环依赖通常指的是单例&#xff08;Singleton&#…

使用 Idea 快速搭建 SpringMVC 项目的详细步骤

一、开篇 SpringMVC 是一款当下流行的优秀的 MVC 框架&#xff0c;关于 MVC 的概念、作用、优点等内容介绍&#xff0c;在作者之前的一篇 Chat 《深入理解 MVC 框架原理&#xff1a;自定义 Struts2 框架》中有详细的描述。描述了关于另一款主流 MVC 框架的原理介绍&#xff0c;…

Docker-Container

Docker ①什么是容器②为什么需要容器③容器的生命周期容器 OOM容器异常退出容器暂停 ④容器命令清单总览docker createdocker rundocker psdocker logsdocker attachdocker execdocker startdocker stopdocker restartdocker killdocker topdocker statsdocker container insp…

unrealbuildtool 无法找到,执行 Generate Visual Studio Project 错误

参考链接 Generate cpp project Couldnt find UnrealBuildTool - Pipeline & Plugins / Plugins - Epic Developer Community Forums (unrealengine.com) 错误提示如下图&#xff1a; 解决方案&#xff1a; 打开 UnrealBuildTool&#xff0c;生成解决方案就可以了

学习Fast-LIO系列代码中相关概念理解

目录 一、流形和流形空间&#xff08;姿态&#xff09; 1.1 定义 1.2 为什么要有流形? 1.3 流形要满足什么性质&#xff1f; (1) 拓扑同胚 (2) 可微结构 1.4 欧式空间和流形空间的区别和联系? (1) 区别&#xff1a; (2) 联系&#xff1a; 1.5 将姿态定义在流形上比…

【Python实用标准库】argparser使用教程

argparser使用教程 1.介绍2.基本使用3.add_argument() 参数设置4.参考 1.介绍 &#xff08;一&#xff09;argparse 模块是 Python 内置的用于命令项选项与参数解析的模块&#xff0c;其用主要在两个方面&#xff1a; 一方面在python文件中可以将算法参数集中放到一起&#x…

CavalierContours 二维线操作

CavalierContours 二维线操作 2D polyline library for offsetting, combining, etc. 用于偏移、交并补等组合等操作的 2D 多折段线库。 Polyline Structure 多段线结构 Polylines are defined by a sequence of vertexes and a bool indicating whether the polyline is cl…

关系型数据库mysql(7)sql高级语句①

目录 一.MySQL常用查询 1.按关键字&#xff08;字段&#xff09;进行升降排序 按分数排序 &#xff08;默认为升序&#xff09; 按分数升序显示 按分数降序显示 根据条件进行排序&#xff08;加上where&#xff09; 根据多个字段进行排序 ​编辑 2.用或&#xff08;or&…

【C语言】内存函数(memmove)的使用和模拟实现

目录 前言memmove定义1.在cplusplus中的定义 memmove的模拟实现1、思路2、难点3、解决方法 模拟实现代码 前言 这篇文章讲述了memcpy的使用、模拟实现和一个未解决的问题内存函数(memcpy)的使用和模拟实现 当我们使用我们模拟的my_memcpy拷贝&#xff0c;当源拷贝地址与目标拷…

C语言编译与链接

前言 我们想一个问题&#xff0c;我们写的C语言代码都是文本信息&#xff0c;电脑能直接执行c语言代码吗&#xff1f;肯定不能啊&#xff0c;计算机能执行的是二进制指令&#xff0c;所以将C语言转化为二进制指令需要一段过程&#xff0c;这篇博客讲一下编译与链接&#xff0c;…

ISAC代码仿真学习笔记

文章目录 A. MIMO Communication ModelB. MIMO Radar Model III. Joint Waveform and Phase Shift Matrix Design for Given Radar BeampatternA. Problem FormulationB. Proposed Algorithm IV. JOINT DESIGN WITH TRADE-OFF BETWEEN RADAR AND COMMUNICATION PERFORMANCEA. P…