Qt model view framework: QListView, QListWidget

QListView

1. Description

QListView presents the items stored in the model as a simple non-hierarchical list or collection of icons. This view does not display horizontal or vertical headers.

QStandardItemModel * model = new QStandardItemModel;
for(int i = 0; i < 11; ++i)
{
QStandardItem *item = new QStandardItem(QString::number(i));
model->appendRow(item);
}
QListView listView;
listView.setModel(model);
listView.show();

Two, type members

1. enum QListView::Flow: The flow direction of the item layout.

  • LeftToRight: Items are arranged from left to right in the view. If the isWrapping property is true, the layout will wrap when it reaches the right side of the visible area.
  • TopToBottom: Items are arranged from top to bottom in the view. (default)

2. enum QListView::LayoutMode: Whether the layout of the item should happen immediately or delayed.

  • SinglePass: Items are laid out all at once.
  • Batched: Items are laid out in batches according to the batchSize property.

3. enum QListView::Movement: How to move items in the view

  • Static: Items cannot be moved. (default)
  • Free: The item can be moved freely.
  • Snap: Items snap to the specified grid when moved.

4. enum QListView::ResizeMode: Whether to re-layout the item when the view is resized.

Fixed: Items are only laid out when the view is shown for the first time.

Adjust: Items are laid out every time the view is resized.

5. enum QListView::ViewMode: view mode

  • ListMode: Items use top-to-bottom flow layout, small size, drag and drop disabled.
  • IconMode: Items use a left-to-right flow layout, large sizes, drag and drop enabled.

Three, attribute members

1、batchSize : int

If layoutMode is set to Batched, this property holds the number of items laid out in each batch. The default value is 100.

2、gridSize : QSize

This property is the size of the grid on which items are laid out. Defaults to an empty size, which means there is no grid and the layout is not done in the grid. Setting this property to a non-null size turns on grid layout. (The spacing property is ignored when grid layout is in effect.)

3、isWrapping : bool

This property holds whether the layout should wrap when there is no more room in the visible area. The point the layout wraps around depends on the flow(QListView::Flow) property. By default, this property is false.

4、itemAlignment : Qt::Alignment

This property holds the alignment of each item in its cell. This is only supported when you have a TopToBottom flow and isWrapping is true. Defaults to 0, i.e. the item completely fills its cell.

5、modelColumn : int

This property holds the columns visible in the model.

6、selectionRectVisible : bool

Whether the selection rectangle is visible if the selection mode is in a mode where multiple items can be selected. The default is false. (QAbstractItemView::SelectionMode)

7、spacing : int

This property is the size of the empty space to pad around items in the layout. The default is 0.

8、uniformItemSizes : bool

This property holds whether all items in the list view have the same size. The default is false.

9、wordWrap : bool

If this property is true, the item text will wrap at necessary word breaks. The default is false.

4. Member functions

1、[signal] void indexesMoved(const QModelIndexList &indexes)

This signal is emitted when the specified index is moved within the view.

2、void clearPropertyFlags()

Clears the set QListView::ViewMode.

3、bool isRowHidden(int row)

Whether a row is hidden.

4、QRect rectForIndex(const QModelIndex &index)

Returns the rectangle of the item at index position in the model.

5、void setRowHidden(int row, bool hide)

Set a line to hide or show.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QT embedded development, Quick module, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

QListWidget

1. Description

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

There are two ways to add items to a list:

Can be built with a list widget as its parent widget.

QListWidget *listWidget = new QListWidget(this);
new QListWidgetItem(tr("Oak"), listWidget);
new QListWidgetItem(tr("Fir"), listWidget);
new QListWidgetItem(tr("Pine"), listWidget);

Can be built without a parent widget and added to the list later.

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

Two, attribute members

1、count : const int

This property holds the number of items in the list, including any hidden items.

2、currentRow : int

This property holds the current item's row.

3、sortingEnabled : bool

This property holds whether sorting is enabled. The default value is false.

3. Member functions

3.1. Signal

1、void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)

This signal is emitted whenever the current item changes.

previous is the previously focused item; current is the new current item.

2、void currentRowChanged(int currentRow)

This signal is emitted whenever the current item changes. currentRow is the current item's row. currentRow is -1 if there is no current item.

3、void currentTextChanged(const QString ¤tText)

This signal is emitted whenever the current item changes. currentText is the text data in the current item. currentText has no effect if there is no current item.

4、void itemActivated(QListWidgetItem *item)

This signal is emitted when the item is activated. The item will be activated when the user clicks or double-clicks it, depending on the system configuration. It is also activated when the user presses the activation key (on Windows and X11 this is the Return key, on Mac OS X it is Command+O).

5、void itemChanged(QListWidgetItem *item)

This signal is emitted whenever item's data changes.

6、void itemClicked(QListWidgetItem *item)

This signal is emitted with the specified item when the mouse button is clicked on the item in the widget.

7、void itemDoubleClicked(QListWidgetItem *item)

This signal is emitted with the specified item when the mouse button is double-clicked on the item in the widget.

8、 void itemEntered(QListWidgetItem *item)

This signal is emitted when the mouse cursor enters an item. item is the input item. This signal is only emitted when mouseTracking is turned on (QWidget::setMouseTracking(true)) or when the mouse is pressed while moving to an item.

9、void itemPressed(QListWidgetItem *item)

This signal is emitted with the specified item when the mouse button is pressed on the item in the widget.

10、void itemSelectionChanged()

This signal is emitted whenever the selection changes.

3.2, function

1、void clear()

Deletes all items in the view.

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

If necessary, scroll the view to make sure the item is visible. The hint specifies where the item should be after the operation.

3、void addItem(const QString &label) / void addItems(const QStringList &labels)

Inserts an item with a text label at the end of the list widget.

4、void addItem(QListWidgetItem *item)

Inserts an item at the end of the list widget.

Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.

5、void closePersistentEditor(QListWidgetItem *item)

Close the persistent editor for the given item.

6、QListWidgetItem * currentItem()

Get the current item.

7、void editItem(QListWidgetItem *item)

If the item is editable, start editing the item.

8、QList<QListWidgetItem *> findItems(const QString &text, Qt::MatchFlags flags)

Use flags to find items with text matching text.

  • enum Qt::MatchFlag: This enum describes the match types that can be used when searching for items in the model.
  • Qt::MatchExactly: Performs QVariant-based matching.
  • Qt::MatchFixedString: Performs string-based matching. String-based comparisons are case-insensitive unless the MatchCaseSensitive flag is also specified.
  • Qt::MatchContains: The search term is contained in items.
  • Qt::MatchStartsWith: Starts of search term matches.
  • Qt::MatchEndsWith: End of search term matches.
  • Qt::MatchCaseSensitive: The search is case sensitive.
  • Qt::MatchRegularExpression: Performs string-based matching using regular expressions as search terms. Use QRegularExpression. When using this flag, a QRegularExpression object can be passed as an argument and will be used directly to perform the search. The case sensitive flag will be ignored.
  • Qt::MatchWildcard: Performs string-based matching using strings with wildcards as search terms.
  • Qt::MatchWrap: Performs a wraparound search so that when the search reaches the last item in the model, it starts again with the first item and continues until all items have been checked.
  • Qt::MatchRecursive: Searches the entire hierarchy.

Notice:

  • Qt::MatchExactly
  • Qt::MatchContains
  • Qt::MatchStartsWith
  • Qt::MatchEndsWith
  • Qt::MatchRegularExpression
  • Qt::MatchWildcard
  • Qt::MatchFixedString

are mutually exclusive. The behavior achieved by setting several of them in the Qt::MatchFlags parameter is undefined.

9、QModelIndex indexFromItem(const QListWidgetItem *item) 【常用】

Returns the QModelIndex associated with the given item.

10、QListWidgetItem * itemFromIndex(const QModelIndex &index) 【常用】

Returns a pointer to the QListWidgetItem associated with the given index.

11、void insertItem(int row, QListWidgetItem *item)

void insertItem(int row, const QString &label)
void insertItems(int row, const QStringList &labels)

Inserts the item at the position in the list given by row.

12、bool isPersistentEditorOpen(QListWidgetItem *item)

Returns whether a persistent editor is opened for the project item.

13、QListWidgetItem * item(int row)

Returns the item of the given row in the list.

14 QListWidgetItem * itemAt ( const QPoint &p ) ;

Returns a pointer to the item at coordinate p. Coordinates are relative to the list widget's viewport() ( QWidget::viewport() ).

15、QWidget * itemWidget(QListWidgetItem *item)

void setItemWidget(QListWidgetItem *item, QWidget *widget)

Returns the widgets displayed in the given item. /

Sets the widgets to be displayed in the given item. This function should only be used to display static content in place of list widget items. If you want to display custom dynamic content or implement a custom editor widget, use QListView and subclass QStyledItemDelegate instead.

16、void removeItemWidget(QListWidgetItem *item)

Remove the widget set on the given item.

17、QList<QListWidgetItem *> items(const QMimeData *data)

Returns a list of pointers to the items containing data.

18、void openPersistentEditor(QListWidgetItem *item)

Open the editor for the given item. The editor remains open after editing.

19、int row(const QListWidgetItem *item)

Returns the rows containing the given item.

20、QList<QListWidgetItem *> selectedItems()

Returns a list of all selected items in the list widget.

21、void setCurrentItem(QListWidgetItem *item)

Set the current item as item. The item is also selected unless the selection mode is NoSelection.

22、void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command)

void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command)

Set the current row.

23、void sortItems(Qt::SortOrder order = Qt::AscendingOrder)

Sorts all items in the list widget according to the specified order.

24、QListWidgetItem * takeItem(int row)

Removes and returns an item from a given row of a list widget.

Items removed from the list widget are not managed by Qt and need to be removed manually.

25、QRect visualItemRect(const QListWidgetItem *item)

Returns the rectangle on the viewport that the item at item occupies.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QT embedded development, Quick module, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Original link: https://blog.csdn.net/kenfan1647/article/details/119700732

Guess you like

Origin blog.csdn.net/hw5230/article/details/131967563