📄 qabstractitemview.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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 "qabstractitemview.h"#ifndef QT_NO_ITEMVIEWS#include <qpointer.h>#include <qapplication.h>#include <qclipboard.h>#include <qpainter.h>#include <qstyle.h>#include <qdrag.h>#include <qevent.h>#include <qscrollbar.h>#include <qwhatsthis.h>#include <qtooltip.h>#include <qdatetime.h>#include <qlineedit.h>#include <qspinbox.h>#include <qitemdelegate.h>#include <private/qabstractitemview_p.h>#include <private/qabstractitemmodel_p.h>#ifndef QT_NO_ACCESSIBILITY#include <qaccessible.h>#endifQAbstractItemViewPrivate::QAbstractItemViewPrivate() : model(QAbstractItemModelPrivate::staticEmptyModel()), itemDelegate(0), selectionModel(0), selectionMode(QAbstractItemView::ExtendedSelection), selectionBehavior(QAbstractItemView::SelectItems), currentlyCommittingEditor(0), pressedModifiers(Qt::NoModifier), pressedPosition(QPoint(-1, -1)), state(QAbstractItemView::NoState), editTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed), lastTrigger(QAbstractItemView::NoEditTriggers), tabKeyNavigation(false),#ifndef QT_NO_DRAGANDDROP showDropIndicator(true), dragEnabled(false), dragDropMode(QAbstractItemView::NoDragDrop), overwrite(false), dropIndicatorPosition(QAbstractItemView::OnItem),#endif autoScroll(true), autoScrollMargin(16), autoScrollCount(0), alternatingColors(false), textElideMode(Qt::ElideRight), verticalScrollMode(QAbstractItemView::ScrollPerItem), horizontalScrollMode(QAbstractItemView::ScrollPerItem), currentIndexSet(false), wrapItemText(false){}QAbstractItemViewPrivate::~QAbstractItemViewPrivate(){}void QAbstractItemViewPrivate::init(){ Q_Q(QAbstractItemView); q->setItemDelegate(new QItemDelegate(q)); q->verticalScrollBar()->setRange(0, 0); q->horizontalScrollBar()->setRange(0, 0); QObject::connect(q->verticalScrollBar(), SIGNAL(actionTriggered(int)), q, SLOT(verticalScrollbarAction(int))); QObject::connect(q->horizontalScrollBar(), SIGNAL(actionTriggered(int)), q, SLOT(horizontalScrollbarAction(int))); QObject::connect(q->verticalScrollBar(), SIGNAL(valueChanged(int)), q, SLOT(verticalScrollbarValueChanged(int))); QObject::connect(q->horizontalScrollBar(), SIGNAL(valueChanged(int)), q, SLOT(horizontalScrollbarValueChanged(int))); viewport->setBackgroundRole(QPalette::Base); doDelayedItemsLayout(); q->setAttribute(Qt::WA_InputMethodEnabled);}/*! \class QAbstractItemView \brief The QAbstractItemView class provides the basic functionality for item view classes. \ingroup model-view \mainclass QAbstractItemView class is the base class for every standard view that uses a QAbstractItemModel. QAbstractItemView is an abstract class and cannot itself be instantiated. It provides a standard interface for interoperating with models through the signals and slots mechanism, enabling subclasses to be kept up-to-date with changes to their models. This class provides standard support for keyboard and mouse navigation, viewport scrolling, item editing, and selections. The QAbstractItemView class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. The view classes that inherit QAbstractItemView only need to implement their own view-specific functionality, such as drawing items, returning the geometry of items, finding items, etc. QAbstractItemView provides common slots such as edit() and setCurrentIndex(). Many protected slots are also provided, including dataChanged(), rowsInserted(), rowsAboutToBeRemoved(), selectionChanged(), and currentChanged(). The root item is returned by rootIndex(), and the current item by currentIndex(). To make sure that an item is visible use scrollTo(). Some of QAbstractItemView's functions are concerned with scrolling, for example setHorizontalScrollMode() and setVerticalScrollMode(). To set the range of the scroll bars, you can, for example, reimplement the view's resizeEvent() function: \code void MyView::resizeEvent(QResizeEvent *event) { horizontalScrollBar()->setRange(0, realWidth - width()); ... } \endcode Note that the range is not updated until the widget is shown. Several other functions are concerned with selection control; for example setSelectionMode(), and setSelectionBehavior(). This class provides a default selection model to work with (selectionModel()), but this can be replaced by using setSelectionModel() with an instance of QItemSelectionModel. For complete control over the display and editing of items you can specify a delegate with setItemDelegate(). QAbstractItemView provides a lot of protected functions. Some are concerned with editing, for example, edit(), and commitData(), whilst others are keyboard and mouse event handlers. \sa {View Classes}, {Model/View Programming}, QAbstractItemModel, {Chart Example}*//*! \enum QAbstractItemView::SelectionMode This enum indicates how the view responds to user selections: \value SingleSelection When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item. \value ContiguousSelection When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. \value ExtendedSelection When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them. \value MultiSelection When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them. \value NoSelection Items cannot be selected. The most commonly used modes are SingleSelection and ExtendedSelection.*//*! \enum QAbstractItemView::SelectionBehavior \value SelectItems Selecting single items. \value SelectRows Selecting only rows. \value SelectColumns Selecting only columns.*//*! \enum QAbstractItemView::ScrollHint \value EnsureVisible Scroll to ensure that the item is visible. \value PositionAtTop Scroll to position the item at the top of the viewport. \value PositionAtBottom Scroll to position the item at the bottom of the viewport. \value PositionAtCenter Scroll to position the item at the center of the viewport.*//*! \enum QAbstractItemView::EditTrigger This enum describes actions which will initiate item editing. \value NoEditTriggers No editing possible. \value CurrentChanged Editing start whenever current item changes. \value DoubleClicked Editing starts when an item is double clicked. \value SelectedClicked Editing starts when clicking on an already selected item. \value EditKeyPressed Editing starts when the platform edit key has been pressed over an item. \value AnyKeyPressed Editing starts when any key is pressed over an item. \value AllEditTriggers Editing starts for all above actions.*//*! \enum QAbstractItemView::CursorAction This enum describes the different ways to navigate between items, \sa moveCursor() \value MoveUp Move to the item above the current item. \value MoveDown Move to the item below the current item. \value MoveLeft Move to the item left of the current item. \value MoveRight Move to the item right of the current item. \value MoveHome Move to the top-left corner item. \value MoveEnd Move to the bottom-right corner item. \value MovePageUp Move one page up above the current item. \value MovePageDown Move one page down below the current item. \value MoveNext Move to the item after the current item. \value MovePrevious Move to the item before the current item.*//*! \enum QAbstractItemView::State Describes the different states the view can be in. This is usually only interesting when reimplementing your own view. \value NoState The is the default state. \value DraggingState The user is dragging items. \value DragSelectingState The user is selecting items. \value EditingState The user is editing an item in a widget editor. \value ExpandingState The user is opening a branch of items. \value CollapsingState The user is closing a branch of items. \value AnimatingState The item view is performing an animation.*//*! \since 4.2 \enum QAbstractItemView::ScrollMode \value ScrollPerItem The view will scroll the contents one item at a time. \value ScrollPerPixel The view will scroll the contents one pixel at a time.*//*! \fn QRect QAbstractItemView::visualRect(const QModelIndex &index) const = 0 Returns the rectangle on the viewport occupied by the item at \a index. If your item is displayed in several areas then visualRect should return the primary area that contains index and not the complete area that index might encompasses, touch or cause drawing. In the base class this is a pure virtual function. \sa indexAt(), visualRegionForSelection()*//*! \fn void QAbstractItemView::scrollTo(const QModelIndex &index, ScrollHint hint) = 0 Scrolls the view if necessary to ensure that the item at \a index is visible. The view will try to position the item according to the given \a hint. In the base class this is a pure virtual function.*//*! \fn QModelIndex QAbstractItemView::indexAt(const QPoint &point) const = 0 Returns the model index of the item at the viewport coordinates \a point. In the base class this is a pure virtual function. \sa visualRect()*//*! \fn void QAbstractItemView::activated(const QModelIndex &index) This signal is emitted when the item specified by \a index is activated by the user. How to activate items depends on the platform; e.g., by single- or double-clicking the item, or by pressing the Return or Enter key when the item is current. \sa clicked(), doubleClicked(), entered(), pressed()*//*! \fn void QAbstractItemView::entered(const QModelIndex &index) This signal is emitted when the mouse cursor enters the item specified by \a index. Mouse tracking needs to be enabled for this feature to work. \sa viewportEntered(), activated(), clicked(), doubleClicked(), pressed()*//*! \fn void QAbstractItemView::viewportEntered() This signal is emitted when the mouse cursor enters the viewport. Mouse tracking needs to be enabled for this feature to work. \sa entered()*//*! \fn void QAbstractItemView::pressed(const QModelIndex &index) This signal is emitted when a mouse button is pressed. The item the mouse was pressed on is specified by \a index. The signal is only emitted when the index is valid. \sa activated(), clicked(), doubleClicked(), entered()*//*! \fn void QAbstractItemView::clicked(const QModelIndex &index) This signal is emitted when a mouse button is clicked. The item the mouse was clicked on is specified by \a index. The signal is only emitted when the index is valid. \sa activated(), doubleClicked(), entered(), pressed()*//*! \fn void QAbstractItemView::doubleClicked(const QModelIndex &index) This signal is emitted when a mouse button is double-clicked. The item the mouse was double-clicked on is specified by \a index. The signal is only emitted when the index is valid. \sa clicked(), activated()*//*! \fn QModelIndex QAbstractItemView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) = 0 Moves the cursor in the view according to the given \a cursorAction and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -