QListWidget的使用

QListWidget的使用QListWidgetQListWidget类提供了一个基于item的列表小部件。QListWidget是一个方便的类,它提供了类似于QlistView所具有的列表视图,但是具有增加和删除的功能。QListWidget使用内部模型来管理列表中的每个QListWidgetItem。想要有更灵活的列表视图,请使用具有标准模型的QListView类。QlistWidget有两种方法追加数据,一种

大家好,又见面了,我是你们的朋友全栈君。

QListWidget

QListWidget类提供了一个基于item的列表小部件。QListWidget是一个方便的类,它提供了类似于QlistView所具有的列表视图,但是具有增加和删除的功能。QListWidget使用内部模型来管理列表中的每个QListWidgetItem。想要有更灵活的列表视图,请使用具有标准模型的QListView类。

QlistWidget有两种方法追加数据,一种是一个个增加,还有一种是批量增加:

首先我们对QlistWidget做一个初始化:

    this->setGeometry(100,100,200,200);

    QListWidget*list=newQListWidget(this);

list->setGeometry(50,50,100,100);

 

QlistWidget追加数据方法一

 

    //方法一

    QListWidgetItem*item=newQListWidgetItem;

    item->setText(方法一”);

    list->addItem(item);

这种方法最简单,适合少量添加。比如音乐播放器的列表,用户需要增加一首歌曲到列表,那用这种方法最简单。

QlistWidget追加数据方法二

 

    //方法二

    QStringList  strList;

    strList<<QString(第二行”)<<QString(第三行”)<<QString(第四行”);

    list->addItems(strList);

方法二相比方法一更适合批量添加,比如音乐播放器的用户有一百首歌曲要添加,你可以使用该功能批量添加。

QlistWidget还有另一种void QListWidget::insertItem(int row, QListWidgetItem *item)函数,用来在指定的地方插入数据。要注意的是它有两个参数,其中的row参数,也就是行数,是从0开始计数的这一点和索引一样,所以要特别注意。

addItems()和insertItem()的区别在于前者在末尾追加数据,后者可以在任意位置插入数据。

QlistWidget设置图标

只需要简单的设置即可,QListWidgetItem提供了setIcon()函数来实现。

    //设置图标

    item->setIcon(QIcon(“:/new/prefix1/img/Tux.png”));

 

QlistWidget设置选择方式

QlistWidget默认的选择模式是单选,在更多的时候需要设置多选模式,我们可以用如下的代码:

//指定选择模式

    list->setSelectionMode(QAbstractItemView:: ExtendedSelection);

如果你只看名字,或许会选择QAbstractItemView::MultiSelection作为选择模式,但是QAbstractItemView::ExtendedSelection才是我们常见的模式,也就是以扩展的形式来多选。具体可以自己测试两种的区别。

 

针对选择模式,主要有以下几种:

我就不一一翻译了,英文浅显易懂。

Constant

Value

Description

QAbstractItemView::SingleSelection

1

When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.

QAbstractItemView::ContiguousSelection

4

When 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::ExtendedSelection

3

When 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::MultiSelection

2

When 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::NoSelection

0

Items cannot be selected.

 

有关QlistWidget的信号也简单易懂,比如列表项被单击,当前列表项改变等。用音乐播放器列表来理解,单击相当于用户选择歌曲,列表项改变相当于切换歌曲。

扫描二维码关注公众号:

QListWidget的使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/134972.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月3日 上午11:00
下一篇 2022年5月3日 上午11:00


相关推荐

  • vmware15最新激活码【最新永久激活】2022.02.01[通俗易懂]

    (vmware15最新激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月31日
    142
  • 用Python的turtle库画太极图

    用Python的turtle库画太极图作为一名中医药大学的学生,对太极图那是情有独钟,这不,我刚开始学Python不久,便想着用turtle库画一个太极图,对turtle库的使用还不熟练,代码量可能有点多……代码:importturtler=200#太极半径turtle.pensize(2)#画笔尺寸#将太极的圆心调整至坐标原点turtle.right(90)turtle.penup()#拿起画笔turtle.fd(r)turtle.pendown()#落下画笔turtle.right(90)#调整海

    2022年5月18日
    47
  • iDEA优化配置

    iDEA优化配置iDEA优化配置1.启动优化配置配置idea软件安装目录下的bin/idea.vmoptions文件,根据自己电脑实际修改前三项大小2.自动导包删包配置按下图配置3.方法分割线4.鼠标悬停提示勾选5.代码忽略大小写提示去掉勾选6.窗口多行显示已打开的class7.新建类配置模版8.编码格式9.自动编译…

    2022年5月21日
    85
  • p6spy的使用和配置「建议收藏」

    p6spy的使用和配置「建议收藏」【注意】:本教程适用与springboot项目1.加载jar包<!–sql打印–><!–https://mvnrepository.com/artifact/p6spy/p6spy–><dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId>

    2026年4月17日
    4
  • 用c语言编写2048小游戏_2048小游戏c语言课程设计

    用c语言编写2048小游戏_2048小游戏c语言课程设计C语言实现简单的控制台2048小游戏。

    2022年8月1日
    10
  • 虚拟机进入桌面命令_win7命令激活系统

    虚拟机进入桌面命令_win7命令激活系统一、NeoKylin系统基础命令1.1关机、重启和登出命令shutdown-hnow立即关闭系统init0关闭系统shutdown-tseconds按预定时间关闭系统shutdown-rnow立即重启reboot重启logout注销1.2系统相关信息date显示系统时间arch和uname-m显示处理器架构uname-r显示正在使用的内核版本cat/proc/cpuinfo查看cpu相关信息…

    2022年8月10日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号