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的使用

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

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