Qt学习笔记之常用控件QlistWidget

一、QListWidget Class

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

Header: #include
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 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 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 items(const QMimeData *data) const
virtual QMimeData * mimeData(const QList items) const
virtual QStringList mimeTypes() const
virtual Qt::DropActions supportedDropActions() const

Reimplemented Protected Functions

virtual bool event(QEvent *e) override

Detailed Description

Qt学习笔记之常用控件QlistWidget_第1张图片uploading.4e448015.gif转存失败重新上传取消Qt学习笔记之常用控件QlistWidget_第2张图片

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 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 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 items(const QMimeData *data) const返回同一进程在该列表创建的所有QListWidgetItem
virtual QMimeData *mimeData(const QList &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 ¤tText)当前字符串改变时
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

//一、QListWidget
 
list_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()<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 
#include 
#include 
#include 
#include 

class Widget : public QWidget
{
    Q_OBJECT

public:
    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_第3张图片

 

你可能感兴趣的:(#,Qt学习之路)