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


相关推荐

  • intellij idea javaweb_数据库系统原理教程

    intellij idea javaweb_数据库系统原理教程一,简介Eclipse/MyEclipse确实用起来诸多不爽,准备切换IntelliJIDEA,本篇介绍如何配置IDEA并使用IDEA开发一个Web应用。二,软件下载与安装1,首先下载安装JDK并配置环境变量。JDK安装目录D:\Java\jdk1.7.0_79新建环境变量JAVA_HOME,其值为D:\Java\jdk1.7.0_79环境变量PATH中添加%JAVA_HOME%\bin

    2022年11月1日
  • to program_I Just Want To

    to program_I Just Want To在http://forum.doom9.org/showthread.php?t=109747上看到的一篇文章,讲得不错,翻译一下,以作备忘。原文是:InX264therearetwodeblockingparameters,calledDeblockingstrengthandDeblockingthreshold.Thestrengthp

    2022年9月19日
  • 使用OpenSSL生成证书-nginx「建议收藏」

    使用OpenSSL生成证书-nginx「建议收藏」使用OpenSSL生成证书1、生成RSA密钥的方法opensslgenrsa-des3-outprivkey.pem2048这个命令会生成一个2048位的密钥,同时有一个des3方法加密的密码,如果你不想要每次都输入密码,可以改成:opensslgenrsa-outprivkey.pem2048建议用2048位密钥,少于此可能会不安全或很快将不安全。2、生成一个证书请求opensslreq-new-keyprivkey.pem-outcert.csr这个命令将会

    2022年9月2日
  • JavaScript 判断元素是否在数组中

    JavaScript 判断元素是否在数组中

    2021年11月22日
  • 抽象工厂模式与工厂方法模式有哪些不同_抽象工厂模式java代码

    抽象工厂模式与工厂方法模式有哪些不同_抽象工厂模式java代码Abstract Factory动机实例模式定义结构要点总结笔记动机在软件系统中,经常面临着”一系列相互依赖的对象“的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作如果应对这种变换?如何绕过常规的对象创建方法(new),提供一种”封装机制“来避免客户程序和这种”多系列具体对象创建工作“的紧耦合?实例数据库连接的时候会有很多关联的对象,这些对象是一个整体朴素class EmployeeDAO{public: vector<EmployeeDAO> GetEm

    2022年8月11日
  • Java“魔法”-jstack命令解析

    怕什么真理无穷进一步有近一步的欢喜前情预告在介绍jstack之前,先简单介绍一下jps。因为jps使用相对简单,各位看官看一下便知。jps的作用是显示当前系统的java进程情况,及其id号…

    2022年3月1日

发表回复

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

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