Qt学习笔记之常用控件QlistWidget
一、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 ¤tText) |
| 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 ¤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
//一、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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt学习笔记之样式表
- 下一篇: Qt学习笔记之常用控件QTreeWidg