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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux c++进程间通信_c++多线程通信

    linux c++进程间通信_c++多线程通信Linux下c开发之——线程间通信2016-02-1817:50

    2022年9月2日
    4
  • 看一下MySQL索引类型「建议收藏」

    看一下MySQL索引类型「建议收藏」一、简介MySQL目前主要有以下几种索引类型:1.普通索引2.唯一索引3.主键索引4.组合索引5.全文索引二、语句CREATETABLEtable_name[col_namedatatype][unique|fulltext][index|key][index_name](col_name[length])[asc|desc]1.unique|fulltext为可选参数,分别表示唯一索引、全文索引2.index和key为同义词,两者作用相同,用来指定创建索引3.co

    2022年5月12日
    39
  • sshd_config 中 PermitRootLogin 的探讨

    sshd_config 中 PermitRootLogin 的探讨众所周知,sshd_config是sshd的配置文件,其中PermitRootLogin可以限定root用户通过ssh的登录方式,如禁止登陆、禁止密码登录、仅允许密钥登陆和开放登陆,本文对其中比较罕见的forced-commands-only选项做了介绍及实战。

    2022年6月12日
    24
  • 怎么退出vi编辑界面_centos保存退出vim

    怎么退出vi编辑界面_centos保存退出vim@Linux基础学习如何退出vi编辑页面background今天在虚拟机上练习如何自动挂载的时候,按照提示在命令行输入vi/etc/fstab,如下图:回车之后进入编辑器界面,如图:由于误操作将这个编辑器内的内容修改了,可是我并不想修改任何内容,然后摸索半天也不知道如何撤销修改,或者说不保存退出,于是我在网上寻找到了一些答案。方法网上的方法是按ESC键跳到命令模式,但我在我的虚…

    2022年9月29日
    3
  • 回文数的判断(三种方法)

    回文数的判断(三种方法)最近做了一点关于回文数的总结 首先先写一篇关于回文数判断的几种方法 回文数的概念 即是给定一个数 这个数顺读和逆读都是一样的 例如 121 1221 是回文数 123 1231 不是回文数 方法一 试用情境 处理小数字 使用数学方法 输入的回文数 x 的范围为 x lt 10 9 int 存储 或者 x lt 10 18 longlong 存储的数 数字的范围不大 这里写的是 int 存储情况

    2025年8月21日
    2
  • 生产环境数据库并发数的调整

    生产环境数据库并发数的调整在开发和测试时,我们往往不会很在意数据库相关的一些并发数的配置,因为开发和测试时,系统的并发量并不会很大,因此,是否正确设置这些参数也不会对结果造成什么影响但是,上生产环境后,系统的并发量大大提高,这时,没有注意数据库的并发数配置的话就会使数据库成为系统最终的并发瓶颈。根据我在实际项目中一段时间的并发测试后,发现关于数据库并发数需要配置的几个地方,希望跟大家分享一下,…

    2022年6月13日
    23

发表回复

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

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