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


相关推荐

  • SecureCRTPortable样式设置

    SecureCRTPortable样式设置SecureCRTPortable高大上设置背景设置进入界面——最上方选项——会话选项——外观,进行如下设置设置完成后就会出现高大上的大神界面

    2022年4月29日
    84
  • Centos7关闭selinux命令「建议收藏」

    关闭selinux步骤0x01用vi修改selinux的配置文件vi/etc/selinux/config0x02修改#SELINUX=enforcing为SELINUX=disabled这里就不修改了,注释掉好了,再直接复制修改为SELINUX=disabledselinux的工作模式enforcing强制模式permissive宽容模式disabled关闭什么是selinux?selinux是Linux的一种安全子系统 Linux中的权限管..

    2022年4月18日
    230
  • java中ReentrantLock类的tryLock和tryLock的例子和用法

    java中ReentrantLock类的tryLock和tryLock的例子和用法ReentrantLock类的tryLock和tryLock(时间)马克-to-win:tryLock的方法就是试一下,如果能得到锁,就返回真,如果当时得不到,马上就返回假,绝不等。tryLock(时间)的用法就是在规定的时间内设法得到锁。如果在规定的时间内最终不能得到锁,就返回假。注意,这个方法是可以被打断的,打断后的处理方法和上面的例子lockInterruptibly的处理一样。…

    2022年10月8日
    2
  • 随机梯度下降SGD算法实现_什么是梯度下降法

    随机梯度下降SGD算法实现_什么是梯度下降法随机梯度下降算法(Stochasticgradientdescent,SGD)在神经网络模型训练中,是一种很常见的优化算法。这种算法是基于梯度下降算法产生的,所以要理解随机梯度下降算法,必须要对梯度下降算法有一个全面的理解。梯度下降:这个算法我在之前的博文LogisticRegression的数学推导过程以及Python实现中有详细的说明介绍,这里我们再来简单回顾一下梯度下降算法:假设…

    2025年10月18日
    3
  • centos7 top命令_top命令结果详解

    centos7 top命令_top命令结果详解top命令Linuxtop命令用于实时显示process的动态。top参数详解第一行,任务队列信息**系统当前时间:**13:52:56**系统开机后到现在的总运行时间:**up66

    2022年7月30日
    9
  • SpringBoot全局配置long转String 丢失精度[通俗易懂]

    SpringBoot全局配置long转String 丢失精度[通俗易懂]web项目中,Java后端传过来的Long/long类型,前端JS接收会丢失精度。 **本文推荐第三、第四种方式**第一种方式简单粗暴,将所有的Lang类型,改为String,数据库改成varchar类型;第二种方式自己建个配置类extendsWebMvcConfigurerAdapter已经被弃用,直接实现WebMvcConfigurer该接口就行了@EnableWebMvc@ConfigurationpublicclassWebDataConvertConfigimple

    2022年5月15日
    39

发表回复

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

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