📄 qabstractitemview.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 "qabstractitemview.h"#ifndef QT_NO_ITEMVIEWS#include <qpointer.h>#include <qapplication.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 <qitemdelegate.h>#include <private/qabstractitemview_p.h>QAbstractItemViewPrivate::QAbstractItemViewPrivate() : model(0), delegate(0), selectionModel(0), selectionMode(QAbstractItemView::ExtendedSelection), selectionBehavior(QAbstractItemView::SelectItems), pressedModifiers(Qt::NoModifier), pressedPosition(QPoint(-1, -1)), state(QAbstractItemView::NoState), editTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed), tabKeyNavigation(false),#ifndef QT_NO_DRAGANDDROP showDropIndicator(false), dragEnabled(false), dropIndicatorPosition(QAbstractItemView::OnItem),#endif autoScroll(true), autoScrollMargin(16), autoScrollInterval(50), autoScrollCount(0), alternatingColors(false), textElideMode(Qt::ElideRight){}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); q->setHorizontalStepsPerItem(64); q->setVerticalStepsPerItem(64); doDelayedItemsLayout(); q->setAttribute(Qt::WA_InputMethodEnabled);}/*! \class QAbstractItemView qabstractitemview.h \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(), columnsInserted(), _q_columnsRemoved(), 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 setHorizontalFactor() and setVerticalFactor(). 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. When implimenting a view that will have scrollbars you want to overload resizeEvent to set the scrollbars range so they will turn on and off, for example: \code horizontalScrollBar()->setRange(0, realWidth - width()); \endcode Note that QAbstractScrollView wont turn on/off the scroolbars based upon the ranges until the widget is shown. They should be manually turned on and off in if other functions require that information. 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 {Model/View Programming}, QAbstractItemModel*//*! \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. \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.*//*! \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 an 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.*//*! \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.*//*! \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 \overload Returns the model index of the item at point \a point. In the base class this is a pure virtual function.*//*! \fn void QAbstractItemView::activated(const QModelIndex &index) This signal is emitted when the item specified by \a index is activated by the user (e.g., by single- or double-clicking the item, depending on the platform). \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.*//*! \fn QModelIndex QAbstractItemView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) = 0 Moves the cursor in the view according to the given \a cursorAction and keyboard modifiers specified by \a modifiers.*//*! \fn int QAbstractItemView::horizontalOffset() const = 0 Returns the horizontal offset of the view. In the base class this is a pure virtual function.*//*! \fn int QAbstractItemView::verticalOffset() const = 0 Returns the vertical offset of the view. In the base class this is a pure virtual function.*//*! \fn bool QAbstractItemView::isIndexHidden(const QModelIndex &index) const Returns true if the item refered to by the given \a index is hidden in the view, otherwise returns false. Hiding is a view specific feature. For example in TableView a column can be marked as hidden or a row in the TreeView. In the base class this is a pure virtual function.*//*! \fn void QAbstractItemView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags) Applies the selection \a flags to the items in or touched by the rectangle, \a rect. When implementing your own itemview setSelection should call selectionModel()->select(selection, flags) where selection is either an empty QModelIndex or a QItemSelection that contains all items that are contained in \a rect. \sa selectionCommand()*//*! \fn QRegion QAbstractItemView::visualRegionForSelection(const QItemSelection &selection) const = 0 Returns the region from the viewport of the items in the given \a selection.*//*! Constructs an abstract item view with the given \a parent.*/QAbstractItemView::QAbstractItemView(QWidget *parent) : QAbstractScrollArea(*(new QAbstractItemViewPrivate), parent){ d_func()->init();}/*! \internal*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -