一、QListWidget Class

The QListWidget class provides an item-based list widget. More...

Header: #include <QListWidget>
qmake: QT += widgets
Inherits: QListView
  • List of all members, including inherited members
  • Obsolete members

Properties

  • count : const int
  • currentRow : int
  • sortingEnabled : bool

Public Functions

  QListWidget(QWidget *parent = nullptr)
virtual ~QListWidget()
void addItem(const QString &label)
void addItem(QListWidgetItem *item)
void addItems(const QStringList &labels)
void closePersistentEditor(QListWidgetItem *item)
int count() const
QListWidgetItem * currentItem() const
int currentRow() const
void editItem(QListWidgetItem *item)
QList<QListWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags) const
void insertItem(int row, QListWidgetItem *item)
void insertItem(int row, const QString &label)
void insertItems(int row, const QStringList &labels)
bool isPersistentEditorOpen(QListWidgetItem *item) const
bool isSortingEnabled() const
QListWidgetItem * item(int row) const
QListWidgetItem * itemAt(const QPoint &p) const
QListWidgetItem * itemAt(int x, int y) const
QWidget * itemWidget(QListWidgetItem *item) const
void openPersistentEditor(QListWidgetItem *item)
void removeItemWidget(QListWidgetItem *item)
int row(const QListWidgetItem *item) const
QList<QListWidgetItem *> selectedItems() const
void setCurrentItem(QListWidgetItem *item)
void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)
void setCurrentRow(int row)
void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)
void setItemWidget(QListWidgetItem *item, QWidget *widget)
void setSortingEnabled(bool enable)
void sortItems(Qt::SortOrder order = Qt::AscendingOrder)
QListWidgetItem * takeItem(int row)
QRect visualItemRect(const QListWidgetItem *item) const

Reimplemented Public Functions

virtual void dropEvent(QDropEvent *event) override
virtual void setSelectionModel(QItemSelectionModel *selectionModel) override

Public Slots

void clear()
void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)

Signals

void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
void currentRowChanged(int currentRow)
void currentTextChanged(const QString &currentText)
void itemActivated(QListWidgetItem *item)
void itemChanged(QListWidgetItem *item)
void itemClicked(QListWidgetItem *item)
void itemDoubleClicked(QListWidgetItem *item)
void itemEntered(QListWidgetItem *item)
void itemPressed(QListWidgetItem *item)
void itemSelectionChanged()

Protected Functions

virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
QModelIndex indexFromItem(const QListWidgetItem *item) const
QListWidgetItem * itemFromIndex(const QModelIndex &index) const
QList<QListWidgetItem *> items(const QMimeData *data) const
virtual QMimeData * mimeData(const QList<QListWidgetItem *> items) const
virtual QStringList mimeTypes() const
virtual Qt::DropActions supportedDropActions() const

Reimplemented Protected Functions

virtual bool event(QEvent *e) override

Detailed Description

转存失败重新上传取消

QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list.

For a more flexible list view widget, use the QListView class with a standard model.

List widgets are constructed in the same way as other widgets:

    QListWidget *listWidget = new QListWidget(this);

The selectionMode() of a list widget determines how many of the items in the list can be selected at the same time, and whether complex selections of items can be created. This can be set with the setSelectionMode() function.

There are two ways to add items to the list: they can be constructed with the list widget as their parent widget, or they can be constructed with no parent widget and added to the list later. If a list widget already exists when the items are constructed, the first method is easier to use:

    new QListWidgetItem(tr("Oak"), listWidget);new QListWidgetItem(tr("Fir"), listWidget);new QListWidgetItem(tr("Pine"), listWidget);

If you need to insert a new item into the list at a particular position, then it should be constructed without a parent widget. The insertItem() function should then be used to place it within the list. The list widget will take ownership of the item.

    QListWidgetItem *newItem = new QListWidgetItem;newItem->setText(itemText);listWidget->insertItem(row, newItem);

For multiple items, insertItems() can be used instead. The number of items in the list is found with the count() function. To remove items from the list, use takeItem().

The current item in the list can be found with currentItem(), and changed with setCurrentItem(). The user can also change the current item by navigating with the keyboard or clicking on a different item. When the current item changes, the currentItemChanged() signal is emitted with the new current item and the item that was previously current.

See also QListWidgetItem, QListView, QTreeView, Model/View Programming, and Tab Dialog Example.

二、QListWidget常用设置

隐藏滚动条:list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
横向排列ui->listWidget->setFlow(QListWidget::LeftToRight); 
在列表末尾插入字符串 void addItem(const QString &label)
在列表末尾插入Item void addItem(QListWidgetItem *item)
在列表末尾插入多个字符串 void addItems(const QStringList &labels)
关闭列表文本编辑 void closePersistentEditor(QListWidgetItem *item)
返回当前有count行 int count() const
返回当前选择的Item QListWidgetItem *currentItem() const
返回当前选择的行数 int currentRow() const
如果该Item可以修改,那么对它进行修改 void editItem(QListWidgetItem *item)
查找与给定标记的字符串匹配的Item QList<QListWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags) const
把Item插入指定行 void insertItem(int row, QListWidgetItem *item)
把字符串插入指定行 void insertItem(int row, const QString &label)
void insertItems(int row, const QStringList &labels)把多个字符串插入指定行
bool isSortingEnabled() const是否启用排序,默认false不启用
QListWidgetItem *item(int row) const返回row行的Item
QListWidgetItem *itemAt(const QPoint &p) const返回相对坐标 P 的Item
QListWidgetItem *itemAt(int x, int y) const返回该X,Y坐标的Item
QWidget *itemWidget(QListWidgetItem *item) const返回Item中的QWidget
void openPersistentEditor(QListWidgetItem *item)打开该Item的编辑器,编辑后默认持续开启,可调用closePersistentEditor关闭
void removeItemWidget(QListWidgetItem *item)移除该Item中的QWidget
int row(const QListWidgetItem *item) const返回指定Item的行
QList<QListWidgetItem *> selectedItems() const返回控件中所有QListWidgetItem
void setCurrentItem(QListWidgetItem *item)设置Item为当前Item,除非除非模式为 NoSelection ,否则选择当前项目
void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)使用指定模式把Item设置为当前项目
void setCurrentRow(int row)根据模式设置当前行
void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)根据给定模式设置当前行
void setItemWidget(QListWidgetItem *item, QWidget *widget)把控件设置为给定的Item中显示
该功能仅可用于列表控件 Item 的位置显示静态内容,如果想显示动态内容使用QListView和子类化QItemDelegate代替
void setSortingEnabled(bool enable)设置是否启用排序功能,默认为false
void sortItems(Qt::SortOrder order = Qt::AscendingOrder)按照指定顺序排列列表
QListWidgetItem *takeItem(int row)删除指定行的Item,并返回该Item
QRect visualItemRect(const QListWidgetItem *item) const返回Item所占的矩形

Protected function:
virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action)处理由外部拖放操作提供的数据,该数据以index中的action的操作结束,如果可以操作返回true
QModelIndex indexFromItem(QListWidgetItem *item) const返回以Item相关的QModelIndex
QListWidgetItem *itemFromIndex(const QModelIndex &index) const返回index相关的QListWidgetItem
QList<QListWidgetItem *> items(const QMimeData *data) const返回同一进程在该列表创建的所有QListWidgetItem
virtual QMimeData *mimeData(const QList<QListWidgetItem *> &items) const返回包含一系列数据Item,所用格式从mimeTypes()得到
virtual QStringList mimeTypes() const返回字符串列
virtual Qt::DropActions supportedDropActions() const返回该Item支持的拖放操作

虚函数:
virtual bool event(QEvent *e)接受一个对象的事件
virtual void dropEvent(QDropEvent *event)拖拽控件

槽:
void clear()删除所有Item
void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible)根据QAbstractItemView::ScrollHint枚举把该Item滚动到指定位置

信号:
void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)当前Item改变时
void currentRowChanged(int currentRow)当前行改变时
void currentTextChanged(const QString &currentText)当前字符串改变时
void itemActivated(QListWidgetItem *item)激活Item时
void itemChanged(QListWidgetItem *item)Item改变时
void itemClicked(QListWidgetItem *item)点击Item时
void itemDoubleClicked(QListWidgetItem *item)双击Item时
void itemEntered(QListWidgetItem *item)Item输入时
void itemPressed(QListWidgetItem *item)Item按下时,即当该Item处于可更改状态,并更改完毕按下回车时
void itemSelectionChanged()更改选择的Item时

三、QListWidget例子1

//一、QListWidgetlist_widget = new QListWidget();//list_widget->resize(200,300);
list_widget->setFixedWidth(300);//设置item图标大小
list_widget->setIconSize(QSize(50,30));QListWidgetItem *add_item = new QListWidgetItem(list_widget);
add_item->setIcon(QIcon(":/res/pix/add.png"));
add_item->setText(tr("Add"));
//设置item项中的文字位置
//add_item->setTextAlignment(Qt::AlignHCenter);
//add_item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);//设置viewModel,来确定使用不同的视图进行显示
//使ListWidgetItem中的图标在上,文字在下
//list_widget->setViewMode(QListView::IconMode);
//list_widget->setViewMode(QListWidget::IconMode);  //这样的形式也可以
//list_widget->setViewMode(QListView::ListMode);//改变item大小,使用QListWidgetItem::setSizeHint()
//add_item->setSizeHint(QSize(60,60));//设置ListWidget可以选择多个item
list_widget->setSelectionMode(QAbstractItemView::ExtendedSelection);//有两种方法在ListWidget中添加item
//一种在构造item的时候,指定其父Widget
QListWidgetItem *cubby_item = new QListWidgetItem(QIcon(":/res/pix/cubby.png"),tr("Cubby"),list_widget);
//第二种方法是在构造完item后,使用QListWidget::additem()来添加item
QListWidgetItem *dropbox_item = new QListWidgetItem();
dropbox_item->setIcon(QIcon(":/res/pix/dropbox.png"));
dropbox_item->setText(tr("Dropbox"));
list_widget->addItem(dropbox_item);//向QListWidget中指定的位置插入itemm,使用QListWidget::addItem()
QListWidgetItem *google_item = new QListWidgetItem(QIcon(":/res/pix/google.png"),tr("Google"));
list_widget->insertItem(1,google_item);//使用QListWidget::takeItem(int index)来删除表中的某一项
//list_widget->takeItem(0);//删除item,必须要加上delete item,否则删不掉
//list_widget->removeItemWidget(add_item);
//delete add_item;//打开和关闭item是否可以编辑,默认不可编辑
//使用QListWidget::openPersistenEditor(QListWidgetItem*)和
//QListWidget::closePersistentEditor(QListWidgetItem*)
//list_widget->openPersistentEditor(cubby_item);//设置当前的item是第几行
//初始化ListWidget显示时,指向哪一行
list_widget->setCurrentRow(1);//设置ListWidget是否可以自动排序,默认是false
//list_widget->setSortingEnabled(true);//设置QLisView大小改变时,图标的调整模式,默认是固定的,可以改成自动调整
//list_widget->setResizeMode(QListView::Adjust);
//设置列表可以拖动,如果想固定不能拖动,使用QListView::Static
//拖动item,进行复制
list_widget->setMovement(QListWidget::Free);QListWidgetItem *computer_item = new QListWidgetItem();
QString str(tr("Computer"));
computer_item->setData(Qt::DisplayRole,str);
computer_item->setIcon(QIcon(":/res/pix/computer.png"));
list_widget->addItem(computer_item);QPushButton *button = new QPushButton(tr("Button"));
QListWidgetItem *button_item = new QListWidgetItem();
list_widget->addItem(button_item);
//实现替换,自定义item
list_widget->setItemWidget(button_item,button);//使用QListWidget::count()来统计ListWidget中总共的item数目
int item_count = list_widget->count();
qDebug()<<item_count;//设置样式,直接在函数中设置
list_widget->setStyleSheet("QListWidget{border:1px solid gray; color:black; }""QListWidget::Item{padding-top:20px; padding-bottom:4px; }""QListWidget::Item:hover{background:skyblue; }""QListWidget::item:selected{background:lightgray; color:red; }""QListWidget::item:selected:!active{border-width:0px; background:lightgreen; }");

四、QListWidget例子2

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QtGui>
#include <QLabel>
#include <QListWidget>
#include <QHBoxLayout>class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();QLabel *label;QListWidget *list;
};#endif // WIDGET_H
#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent)
{label = new QLabel;label->setFixedWidth(70);list = new QListWidget;list->addItem(new QListWidgetItem(QIcon(":/images/line.PNG"), tr("Line")));list->addItem(new QListWidgetItem(QIcon(":/images/rect.PNG"), tr("Rectangle")));list->addItem(new QListWidgetItem(QIcon(":/images/oval.PNG"), tr("Oval")));list->addItem(new QListWidgetItem(QIcon(":/images/tri.PNG"), tr("Triangle")));QHBoxLayout *layout = new QHBoxLayout;layout->addWidget(label);layout->addWidget(list);setLayout(layout);connect(list, SIGNAL(currentTextChanged(QString)), label, SLOT(setText(QString)));
}Widget::~Widget()
{}

Qt学习笔记之常用控件QlistWidget相关推荐

  1. Qt学习笔记之常用控件QTreeWidget

    一.QTreeWidget Class The QTreeWidget class provides a tree view that uses a predefined tree model. Mo ...

  2. 【MFC】学习笔记:常用控件之组合框(Combo Box)

    01.目录 目录 01.目录 02.控件介绍 03.控件的消息通知函数 04.创建组合框控件及成员函数介绍 4.1 组合框的创建 4.2 CComboBox类的主要成员函数 05.应用实例 06.总结 ...

  3. Qt学习笔记-QSS装饰控件

    运行截图如下: QDialog的样式表: QDialog{ background-color:#4CAF50; }QPushButton{background-color: white;border: ...

  4. [QT_015]Qt学习之基于条目控件的自定义特性(拖拽+右键菜单+样式)

    本文转自:<Qt编程指南>        作者:奇先生 Qt编程指南,Qt新手教程,Qt Programming Guide 本节介绍基于条目控件的定制特性,首先介绍条目的拖拽,列表控件. ...

  5. JavaFX 学习笔记——窗口与控件

    前言 如今比较流行的桌面gui框架有WPF.WinForm.Qt.javafx等.其中WPF和WinForm目前还只能在运行Winsows上.Qt(widget)是一个很强大的跨平台C++框架(不只是 ...

  6. vs2010 学习Silverlight学习笔记(7):控件样式与模板

    概要: 终于知道Silverlight--App.xaml是干什么用的了,不仅可以用来封装样式(类似css),还可以制定控件模版...好强大的功能啊. 封装: 继续学习<一步一步学Silverl ...

  7. vb.net listview 删除选定行_VBA学习笔记59-1: listview控件

    学习资源:<Excel VBA从入门到进阶>第59集 by兰色幻想 本节学习Listview控件,它可以用多种视图方式显示项目的控件.由于其外形美观而且非常实用,所以使用频率很高. Lis ...

  8. kendo treeview 修改节点显示值_VBA学习笔记60-1: Treeview控件

    学习资源:<Excel VBA从入门到进阶>第60集 by兰色幻想 本节讲Treeview控件. TreeView控件是以树形结构显示数据的控件.利用TreeView控件,可以设计出树形结 ...

  9. Qt工作笔记-自定义开关控件

    1.自定义开关控件: 2.点击有动画效果: 3.在动画效果中,不再响应信号: 运行截图如下: 输出响应信号: 源码如下: myonoff.h #ifndef MYONOFF_H #define MYO ...

最新文章

  1. android工程中让人很不爽的“×××警告”
  2. BZOJ - 2244 拦截导弹 (dp,CDQ分治+树状数组优化)
  3. VC包含目录、附加依赖项、库目录及具体设置
  4. 句法分析:依存分析(Dependency Parsing)
  5. ROI Pooling层解析
  6. sklearn中的正则化
  7. 2.1.1Remove Duplicates from Sorted Arr
  8. laravel无法运行php,Laravel:php artisan服务无法启动
  9. Obj-C 实现设计模式 -- Adapter
  10. win7 IIS7 HTTP 错误 404.2 - Not Found的解决方法
  11. 0-简单工厂模式类图
  12. ubuntu wps安装
  13. ligerui联动清空控件值
  14. 最好用的发短信(验证码、语音短信)接口
  15. 开发APP的java工具_app傻瓜式开发工具_app开发工具
  16. 引擎磨合 (Break In) 的秘密
  17. java安装安卓模拟器下载安装_Android模拟器安装及其安装APK方法
  18. android动态修改refresh rate,Lenovo k860i 移植Android 4.4 cm11进度记录【下篇--实时更新中】...
  19. hitTest的作用与用法
  20. 暑假学习计划:Day_1.JSPServletTomcat 环境搭建到基础的认识。

热门文章

  1. boost::sort模块实现支持不同分布的随机数生成器的测试程序
  2. boost::rational模块相关的测试程序
  3. boost::mpl模块实现transform_view相关的测试程序
  4. boost::log::parse_formatter用法的测试程序
  5. boost::detail模块实现二分法查找的测试程序
  6. boost::contract模块实现move的测试程序
  7. boost的chrono模块explore limits探索极限的测试程序
  8. C++实现教学信息管理系统
  9. VTK:PolyData之MultiBlockMergeFilter
  10. C语言从已排序的链表中删除重复项(附完整源码)