⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtreeview.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************** 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 "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>#ifndef QT_NO_ACCESSIBILITY#include <qaccessible.h>#endif#include <private/qtreeview_p.h>/*!    \class QTreeView    \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 tree views are constructed using the QHeaderView class    and can be hidden using header()->hide(). Note that each header    is configured with its \l{QHeaderView::}{stretchLastSection}    property set to true, ensuring that the view does not waste any    of the space assigned to it for its header.    \section1 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 Minus  \o Same as LeftArrow.    \row \o RightArrow \o Reveals the children of the current item (if present)         by expanding a branch.    \row \o Plus  \o Same as RightArrow.    \row \o Asterisk  \o Expands all children of the current item (if present).    \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    \section1 Improving Performance    It is possible to give the view hints about the data it is handling in order    to improve its performance when displaying large numbers of items. One approach    that can be taken for views that are intended to display items with equal heights    is to set the \l uniformRowHeights property to true.    \sa QListView, QTreeWidget, {View Classes}, QAbstractItemModel, QAbstractItemView,        {Dir View Example}*//*!  \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) { // support row editing        disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),                   d->model, SLOT(submit()));        disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),                   this, SLOT(rowsRemoved(QModelIndex,int,int)));        disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_modelAboutToBeReset()));    }    d->viewItems.clear();    d->expandedIndexes.clear();    d->hiddenIndexes.clear();    d->header->setModel(model);    QAbstractItemView::setModel(model);    // QAbstractItemView connects to a private slot    disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),               this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));    // do header layout after the tree    disconnect(d->model, SIGNAL(layoutChanged()),               d->header, SLOT(_q_layoutChanged()));    // QTreeView has a public slot for this    connect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),            this, SLOT(rowsRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),            this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)),            this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));    connect(d->model, SIGNAL(modelAboutToBeReset()), SLOT(_q_modelAboutToBeReset()));    if (d->sortingEnabled)        sortByColumn(header()->sortIndicatorSection());}/*!  \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->selectionModel) {        if (d->allColumnsShowFocus) {            QObject::disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),                                this, SLOT(_q_currentChanged(QModelIndex,QModelIndex)));        }        // support row editing        disconnect(d->selectionModel, SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),                   d->model, SLOT(submit()));    }    d->header->setSelectionModel(selectionModel);    QAbstractItemView::setSelectionModel(selectionModel);    if (d->selectionModel) {        if (d->allColumnsShowFocus) {            QObject::connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),                             this, SLOT(_q_currentChanged(QModelIndex,QModelIndex)));        }        // 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_D(QTreeView);    if (header == d->header || !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(d->model);        if (d->selectionModel)            d->header->setSelectionModel(d->selectionModel);    }    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(geometriesChanged()),            this, SLOT(updateGeometries()));    setSortingEnabled(d->sortingEnabled);}/*!  \property QTreeView::autoExpandDelay  \brief The delay time before items in a tree are opened during a drag and drop operation.  \since 4.3  This property holds the amount of time in milliseconds that the user must wait over  a node before that node will automatically open or close.  If the time is  set to less then 0 then it will not be activated.*/int QTreeView::autoExpandDelay() const{    Q_D(const QTreeView);    return d->autoExpandDelay;}void QTreeView::setAutoExpandDelay(int delay){    Q_D(QTreeView);    d->autoExpandDelay = delay;}/*!  \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);    if (i != d->indent) {        d->indent = i;        d->viewport->update();    }}/*!  \property QTreeView::rootIsDecorated  \brief whether to show controls for expanding and collapsing top-level items  Items with children are typically shown with controls to expand and collapse  them, allowing their children to be shown or hidden. If this property is  false, these controls are not shown for top-level items. This can be used to  make a single level tree structure appear like a simple list of items.  By default, this property is true.*/bool QTreeView::rootIsDecorated() const{    Q_D(const QTreeView);    return d->rootDecoration;}void QTreeView::setRootIsDecorated(bool show){    Q_D(QTreeView);    if (show != d->rootDecoration) {        d->rootDecoration = show;        d->viewport->update();    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -