📄 qtreeview.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 "qtreeview.h"#ifndef QT_NO_TREEVIEW#include <qheaderview.h>#include <qitemdelegate.h>#include <qapplication.h>#include <qscrollbar.h>#include <qpainter.h>#include <qstack.h>#include <qstyle.h>#include <qstyleoption.h>#include <qevent.h>#include <qpen.h>#include <qdebug.h>#include <private/qtreeview_p.h>/*! \class QTreeView qtreeview.h \brief The QTreeView class provides a default model/view implementation of a tree view. \ingroup model-view \mainclass A QTreeView implements a tree representation of items from a model. This class is used to provide standard hierarchical lists that were previously provided by the \c QListView class, but using the more flexible approach provided by Qt's model/view architecture. The QTreeView class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. QTreeView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class. It is simple to construct a tree view displaying data from a model. In the following example, the contents of a directory are supplied by a QDirModel and displayed as a tree: \quotefromfile snippets/shareddirmodel/main.cpp \skipto QDirModel *model \printuntil QTreeView *tree \skipto tree->setModel( \printuntil tree->setModel( The model/view architecture ensures that the contents of the tree view are updated as the model changes. Items that have children can be in an expanded (children are visible) or collapsed (children are hidden) state. When this state changes a collapsed() or expanded() signal is emitted with the model index of the relevant item. The amount of indentation used to indicate levels of hierarchy is controlled by the \l indentation property. Headers in a tree view are constructed using the QHeaderView class and can be hidden using header()->hide(). \section2 Key Bindings QTreeView supports a set of key bindings that enable the user to navigate in the view and interact with the contents of items: \table \header \o Key \o Action \row \o UpArrow \o Moves the cursor to the item in the same column on the previous row. If the parent of the current item has no more rows to navigate to, the cursor moves to the relevant item in the last row of the sibling that precedes the parent. \row \o DownArrow \o Moves the cursor to the item in the same column on the next row. If the parent of the current item has no more rows to navigate to, the cursor moves to the relevant item in the first row of the sibling that follows the parent. \row \o LeftArrow \o Hides the children of the current item (if present) by collapsing a branch. \row \o RightArrow \o Reveals the children of the current item (if present) by expanding a branch. \row \o PageUp \o Moves the cursor up one page. \row \o PageDown \o Moves the cursor down one page. \row \o Home \o Moves the cursor to an item in the same column of the first row of the first top-level item in the model. \row \o End \o Moves the cursor to an item in the same column of the last row of the last top-level item in the model. \row \o F2 \o In editable models, this opens the current item for editing. The Escape key can be used to cancel the editing process and revert any changes to the data displayed. \endtable \omit Describe the expanding/collapsing concept if not covered elsewhere. \endomit \table 100% \row \o \inlineimage windowsxp-treeview.png Screenshot of a Windows XP style tree view \o \inlineimage macintosh-treeview.png Screenshot of a Macintosh style tree view \o \inlineimage plastique-treeview.png Screenshot of a Plastique style tree view \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} tree view. \o A \l{Macintosh Style Widget Gallery}{Macintosh style} tree view. \o A \l{Plastique Style Widget Gallery}{Plastique style} tree view. \endtable \sa QListView, QTreeWidget, {Model/View Programming}, QAbstractItemModel, QAbstractItemView*//*! \fn void QTreeView::expanded(const QModelIndex &index) This signal is emitted when the item specified by \a index is expanded.*//*! \fn void QTreeView::collapsed(const QModelIndex &index) This signal is emitted when the item specified by \a index is collapsed.*//*! Constructs a table view with a \a parent to represent a model's data. Use setModel() to set the model. \sa QAbstractItemModel*/QTreeView::QTreeView(QWidget *parent) : QAbstractItemView(*new QTreeViewPrivate, parent){ Q_D(QTreeView); d->initialize();}/*! \internal*/QTreeView::QTreeView(QTreeViewPrivate &dd, QWidget *parent) : QAbstractItemView(dd, parent){ Q_D(QTreeView); d->initialize();}/*! Destroys the tree view.*/QTreeView::~QTreeView(){}/*! \reimp*/void QTreeView::setModel(QAbstractItemModel *model){ Q_D(QTreeView); if (d->selectionModel && d->model) { // support row editing disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), d->model, SLOT(submit())); disconnect(d->model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(rowsRemoved(const QModelIndex &, int, int))); } d->viewItems.clear(); d->expandedIndexes.clear(); d->hiddenIndexes.clear(); d->header->setModel(model); QAbstractItemView::setModel(model); if (d->model) { // QAbstractItemView connects to a private slot disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(rowsRemoved(QModelIndex, int, int))); // QTreeView has a public slot for this connect(d->model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(rowsRemoved(const QModelIndex &, int, int))); }}/*! \reimp*/void QTreeView::setRootIndex(const QModelIndex &index){ Q_D(QTreeView); d->header->setRootIndex(index); QAbstractItemView::setRootIndex(index);}/*! \reimp*/void QTreeView::setSelectionModel(QItemSelectionModel *selectionModel){ Q_D(QTreeView); Q_ASSERT(selectionModel); if (d->model && d->selectionModel) // support row editing disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), d->model, SLOT(submit())); d->header->setSelectionModel(selectionModel); QAbstractItemView::setSelectionModel(selectionModel); if (d->model && d->selectionModel) // support row editing connect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), d->model, SLOT(submit()));}/*! Returns the header for the tree view. \sa QAbstractItemModel::headerData()*/QHeaderView *QTreeView::header() const{ Q_D(const QTreeView); return d->header;}/*! Sets the header for the tree view, to the given \a header. The view takes ownership over the given \a header and deletes it when a new header is set. \sa QAbstractItemModel::headerData()*/void QTreeView::setHeader(QHeaderView *header){ Q_ASSERT(header); Q_D(QTreeView); if (header == d->header) return; if (d->header && d->header->parent() == this) delete d->header; d->header = header; d->header->setParent(this); if (!d->header->model()) d->header->setModel(model()); connect(d->header, SIGNAL(sectionResized(int,int,int)), this, SLOT(columnResized(int,int,int))); connect(d->header, SIGNAL(sectionMoved(int,int,int)), this, SLOT(columnMoved())); connect(d->header, SIGNAL(sectionCountChanged(int,int)), this, SLOT(columnCountChanged(int,int))); connect(d->header, SIGNAL(sectionHandleDoubleClicked(int)), this, SLOT(resizeColumnToContents(int))); connect(d->header, SIGNAL(sectionClicked(int)), this, SLOT(sortByColumn(int))); d->header->setFocusProxy(this);}/*! \property QTreeView::indentation \brief indentation of the items in the tree view. This property holds the indentation measured in pixels of the items for each level in the tree view. For top-level items, the indentation specifies the horizontal distance from the viewport edge to the items in the first column; for child items, it specifies their indentation from their parent items.*/int QTreeView::indentation() const{ Q_D(const QTreeView); return d->indent;}void QTreeView::setIndentation(int i){ Q_D(QTreeView); d->indent = i;}/*! \property QTreeView::rootIsDecorated \brief whether to show controls for expanding and collapsing items This property holds whether root items are displayed with controls for expanding and collapsing them.*/bool QTreeView::rootIsDecorated() const{ Q_D(const QTreeView); return d->rootDecoration;}void QTreeView::setRootIsDecorated(bool show){ Q_D(QTreeView); d->rootDecoration = show; d->viewport->update();}/*! \property QTreeView::uniformRowHeights \brief whether all items in the treeview have the same height This property should only be set to true if it is guaranteed that all items in the view has the same height. This enables the view to do some optimizations.*/bool QTreeView::uniformRowHeights() const{ Q_D(const QTreeView); return d->uniformRowHeights;}void QTreeView::setUniformRowHeights(bool uniform){ Q_D(QTreeView); d->uniformRowHeights = uniform;}/*! \property QTreeView::itemsExpandable \brief whether the items are expandable by the user. This property holds whether the user can expand and collapse items interactively.*/bool QTreeView::itemsExpandable() const{ Q_D(const QTreeView); return d->itemsExpandable;}void QTreeView::setItemsExpandable(bool enable){ Q_D(QTreeView); d->itemsExpandable = enable;}/*! Returns the horizontal position of the \a column in the viewport.*/int QTreeView::columnViewportPosition(int column) const{ Q_D(const QTreeView); return d->header->sectionViewportPosition(column);}/*! Returns the width of the \a column. \sa resizeColumnToContents()*/int QTreeView::columnWidth(int column) const{ Q_D(const QTreeView); return d->header->sectionSize(column);}/*! Returns the column in the tree view whose header covers the \a x coordinate given.*/int QTreeView::columnAt(int x) const{ Q_D(const QTreeView); return d->header->logicalIndexAt(x);}/*! Returns true if the \a column is hidden; otherwise returns false. \sa hideColumn(), isRowHidden()*/bool QTreeView::isColumnHidden(int column) const{ Q_D(const QTreeView); return d->header->isSectionHidden(column);}/*! If \a hide is true the \a column is hidden, otherwise the \a column is shown. \sa hideColumn(), setRowHidden()*/void QTreeView::setColumnHidden(int column, bool hide){ Q_D(QTreeView); if (column < 0 || column >= d->header->count()) return; d->header->setSectionHidden(column, hide);}/*! Returns true if the item in the given \a row of the \a parent is hidden; otherwise returns false. \sa setRowHidden(), isColumnHidden()*/bool QTreeView::isRowHidden(int row, const QModelIndex &parent) const{ Q_D(const QTreeView); if (d->hiddenIndexes.isEmpty()) return false; return d->hiddenIndexes.contains(model()->index(row, 0, parent));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -