📄 qlistview.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qlistview.h"#ifndef QT_NO_LISTVIEW#include <qabstractitemdelegate.h>#include <qapplication.h>#include <qpainter.h>#include <qbitmap.h>#include <qvector.h>#include <qstyle.h>#include <qevent.h>#include <qscrollbar.h>#include <qrubberband.h>#include <private/qlistview_p.h>#include <qdebug.h>/*! \class QListView qlistview.h \brief The QListView class provides a list or icon view onto a model. \ingroup model-view \mainclass A QListView presents items stored in a model, either as a simple non-hierarchical list, or as a collection of icons. This class is used to provide lists and icon views that were previously provided by the \c QListBox and \c QIconView classes, but using the more flexible approach provided by Qt's model/view architecture. The QListView class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. This view does not display horizontal or vertical headers; to display a list of items with a horizontal header, use QTreeView instead. QListView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class. Items in a list view can be displayed using one of two view modes: In \l ListMode, the items are displayed in the form of a simple list; in \l IconMode, the list view takes the form of an \e{icon view} in which the items are displayed with icons like files in a file manager. By default, the list view is in \l ListMode. To change the view mode, use the setViewMode() function, and to determine the current view mode, use viewMode(). Items in these views are laid out in the direction specified by the flow() of the list view. The items may be fixed in place, or allowed to move, depending on the view's movement() state. If the items in the model cannot be completely laid out in the direction of flow, they can be wrapped at the boundary of the view widget; this depends on isWrapping(). This property is useful when the items are being represented by an icon view. The resizeMode() and layoutMode() govern how and when the items are laid out. Items are spaced according to their spacing(), and can exist within a notional grid of size specified by gridSize(). The items can be rendered as large or small icons depending on their iconSize(). \table 100% \row \o \inlineimage windowsxp-listview.png Screenshot of a Windows XP style list view \o \inlineimage macintosh-listview.png Screenshot of a Macintosh style table view \o \inlineimage plastique-listview.png Screenshot of a Plastique style table view \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} list view. \o A \l{Macintosh Style Widget Gallery}{Macintosh style} list view. \o A \l{Plastique Style Widget Gallery}{Plastique style} list view. \endtable \sa {Model/View Programming}, QTreeView, QTableView, QListWidget*//*! \enum QListView::ViewMode \value ListMode The items are layed out using TopToBottom flow, with Small size and Static movement \value IconMode The items are layed out using LeftToRight flow, with Large size and Free movement*//*! \enum QListView::Movement \value Static The items cannot be moved by the user. \value Free The items can be moved freely by the user. \value Snap The items snap to the specified grid when moved; see setGridSize().*//*! \enum QListView::Flow \value LeftToRight The items are laid out in the view from the left to the right. \value TopToBottom The items are laid out in the view from the top to the bottom.*//*! \enum QListView::ResizeMode \value Fixed The items will only be laid out the first time the view is shown. \value Adjust The items will be laid out every time the view is resized.*//*! \enum QListView::LayoutMode \value SinglePass The items are laid out all at once. \value Batched The items are laid out in batches of 100 items.*//*! Creates a new QListView with the given \a parent to view a model. Use setModel() to set the model.*/QListView::QListView(QWidget *parent) : QAbstractItemView(*new QListViewPrivate, parent){ setViewMode(ListMode); setSelectionMode(SingleSelection);}/*! \internal*/QListView::QListView(QListViewPrivate &dd, QWidget *parent) : QAbstractItemView(dd, parent){ setViewMode(ListMode); setSelectionMode(SingleSelection);}/*! Destroys the view.*/QListView::~QListView(){}/*! \property QListView::movement \brief whether the items can be moved freely, are snapped to a grid, or cannot be moved at all. This property determines how the user can move the items in the view. \l Static means that the items can't be moved the user. \l Free means that the user can drag and drop the items to any position in the view. \l Snap means that the user can drag and drop the items, but only to the positions in a notional grid signified by the gridSize property. Setting this property when the view is visible will cause the items to be laid out again. \sa gridSize*/void QListView::setMovement(Movement movement){ Q_D(QListView); d->modeProperties |= uint(QListViewPrivate::Movement); d->movement = movement;#ifndef QT_NO_DRAGANDDROP bool movable = (movement != Static); setDragEnabled(movable); d->viewport->setAcceptDrops(movable);#endif d->doDelayedItemsLayout();}QListView::Movement QListView::movement() const{ Q_D(const QListView); return d->movement;}/*! \property QListView::flow \brief which direction the items layout should flow. If this property is \l LeftToRight, the items will be laid out left to right. If the \l isWrapping property is true, the layout will wrap when it reaches the right side of the visible area. If this property is \l TopToBottom, the items will be laid out from the top of the visible area, wrapping when it reaches the bottom. Setting this property when the view is visible will cause the items to be laid out again.*/void QListView::setFlow(Flow flow){ Q_D(QListView); d->modeProperties |= uint(QListViewPrivate::Flow); d->flow = flow; d->doDelayedItemsLayout();}QListView::Flow QListView::flow() const{ Q_D(const QListView); return d->flow;}/*! \property QListView::isWrapping \brief whether the items layout should wrap. This property holds whether the layout should wrap when there is no more space in the visible area. The point at which the layout wraps depends on the \l flow property. Setting this property when the view is visible will cause the items to be laid out again.*/void QListView::setWrapping(bool enable){ Q_D(QListView); d->modeProperties |= uint(QListViewPrivate::Wrap); d->wrap = enable; d->doDelayedItemsLayout();}bool QListView::isWrapping() const{ Q_D(const QListView); return d->wrap;}/*! \property QListView::resizeMode \brief whether the items are laid out again when the view is resized. If this property is \l Adjust, the items will be laid out again when the view is resized. If the value is \l Fixed, the items will not be laid out when the view is resized.*/void QListView::setResizeMode(ResizeMode mode){ Q_D(QListView); d->resizeMode = mode;}QListView::ResizeMode QListView::resizeMode() const{ Q_D(const QListView); return d->resizeMode;}/*! \property QListView::layoutMode \brief whether the layout of items should happen immediately or be delayed. This property holds the layout mode for the items. When the mode is \l SinglePass (the default), the items are laid out all in one go. When the mode is \l Batched, the items are laid out in batches of 100 items, while processing events. This makes it possible to instantly view and interact with the visible items while the rest are being laid out.*/void QListView::setLayoutMode(LayoutMode mode){ Q_D(QListView); d->layoutMode = mode;}QListView::LayoutMode QListView::layoutMode() const{ Q_D(const QListView); return d->layoutMode;}/*! \property QListView::spacing \brief the space between items in the layout This property is the size of the empty space between items in the layout. Setting this property when the view is visible will cause the items to be laid out again.*/void QListView::setSpacing(int space){ Q_D(QListView); d->modeProperties |= uint(QListViewPrivate::Spacing); d->spacing = space; d->doDelayedItemsLayout();}int QListView::spacing() const{ Q_D(const QListView); return d->spacing;}/*! \property QListView::gridSize \brief the size of the layout grid This property is the size of the grid in which the items are laid out. The default is an empty size which means that there is no grid and the layout is not done in a grid. Setting this property to a non-empty size switches on the grid layout. (When a grid layout is in force the \l spacing property is ignored.) Setting this property when the view is visible will cause the items to be laid out again.*/void QListView::setGridSize(const QSize &size){ Q_D(QListView); d->modeProperties |= uint(QListViewPrivate::GridSize); d->gridSize = size; d->doDelayedItemsLayout();}QSize QListView::gridSize() const{ Q_D(const QListView); return d->gridSize;}/*! \property QListView::viewMode \brief the view mode of the QListView. This property will change the other unset properties to conform with the set view mode. Properties that have already been set will not be changed, unless clearPropertyFlags() has been called.*/void QListView::setViewMode(ViewMode mode){ Q_D(QListView); d->viewMode = mode; if (mode == ListMode) { if (!(d->modeProperties & QListViewPrivate::Wrap)) d->wrap = false; if (!(d->modeProperties & QListViewPrivate::Spacing)) d->spacing = 0; if (!(d->modeProperties & QListViewPrivate::GridSize)) d->gridSize = QSize(); if (!(d->modeProperties & QListViewPrivate::Flow)) d->flow = TopToBottom; if (!(d->modeProperties & QListViewPrivate::Movement)) d->movement = Static; if (!(d->modeProperties & QListViewPrivate::ResizeMode)) d->resizeMode = Fixed; } else { if (!(d->modeProperties & QListViewPrivate::Wrap)) d->wrap = true; if (!(d->modeProperties & QListViewPrivate::Spacing)) d->spacing = 0; if (!(d->modeProperties & QListViewPrivate::GridSize)) d->gridSize = QSize(); if (!(d->modeProperties & QListViewPrivate::Flow)) d->flow = LeftToRight; if (!(d->modeProperties & QListViewPrivate::Movement)) d->movement = Free; if (!(d->modeProperties & QListViewPrivate::ResizeMode)) d->resizeMode = Fixed; }#ifndef QT_NO_DRAGANDDROP bool movable = (d->movement != Static); setDragEnabled(movable); setAcceptDrops(movable);#endif d->doDelayedItemsLayout();}QListView::ViewMode QListView::viewMode() const{ Q_D(const QListView); return d->viewMode;}/*! Clears the property flags. See \l{viewMode}.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -