📄 qheaderview.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 "qheaderview.h"#ifndef QT_NO_ITEMVIEWS#include <qbitarray.h>#include <qbrush.h>#include <qdebug.h>#include <qevent.h>#include <qpainter.h>#include <qscrollbar.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qstyle.h>#include <qstyleoption.h>#include <qvector.h>#include <qapplication.h>#include <qvarlengtharray.h>#include <qabstractitemdelegate.h>#include <qvariant.h>#include <private/qheaderview_p.h>#include <private/qabstractitemmodel_p.h>#ifndef QT_NO_DATASTREAM#include <qdatastream.h>QDataStream &operator<<(QDataStream &out, const QHeaderViewPrivate::SectionSpan &span){ span.write(out); return out;}QDataStream &operator>>(QDataStream &in, QHeaderViewPrivate::SectionSpan &span){ span.read(in); return in;}#endif/*! \class QHeaderView \brief The QHeaderView class provides a header row or header column for item views. \ingroup model-view \mainclass A QHeaderView displays the headers used in item views such as the QTableView and QTreeView classes. It takes the place of the \c QHeader class previously used for the same purpose, but uses the Qt's model/view architecture for consistency with the item view classes. The QHeaderView class is one of the \l{Model/View Classes} and is part of Qt's \l{Model/View Programming}{model/view framework}. The header gets the data for each section from the model using the QAbstractItemModel::headerData() function. You can set the data by using QAbstractItemModel::setHeaderData(). Each header has an orientation() and a number of sections, given by the count() function. A section refers to a part of the header - either a row or a column, depending on the orientation. Sections can be moved and resized using moveSection() and resizeSection(); they can also be hidden and shown with hideSection() and showSection(). Each section of a header is described by a section ID, specified by its section(), and can be located at a particular visualIndex() in the header. A section can have a sort indicator set with setSortIndicator(); this indicates whether the items in the associated item view will be sorted in the order given by the section. For a horizontal header the section is equivalent to a column in the model, and for a vertical header the section is equivalent to a row in the model. \section1 Moving Header Sections A header can be fixed in place, or made movable with setMovable(). It can be made clickable with setClickable(), and has resizing behavior in accordance with setResizeMode(). A header emits sectionMoved() if the user moves a section, sectionResized() if the user resizes a section, and sectionClicked() and sectionHandleDoubleClicked() in response to mouse clicks. A header also emits sectionCountChanged() and sectionAutoResize(). You can identify a section using the logicalIndex() and logicalIndexAt() functions, or by its index position, using the visualIndex() and visualIndexAt() functions. The visual index will change if a section is moved, but the logical index will not change. \section1 Appearance QTableWidget and QTableView create default headers. If you want the headers to be visible, you can use \l{QFrame::}{setVisible()}. \note Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect. \sa {Model/View Programming}, QListView, QTableView, QTreeView*//*! \enum QHeaderView::ResizeMode The resize mode specifies the behavior of the header sections. It can be set on the entire header view or on individual sections using setResizeMode(). \value Interactive The user can resize the section. The section can also be resized programmatically using resizeSection(). The section size defaults to \l defaultSectionSize. (See also \l cascadingSectionResizes.) \value Fixed The user cannot resize the section. The section can only be resized programmatically using resizeSection(). The section size defaults to \l defaultSectionSize. \value Stretch QHeaderView will automatically resize the section to fill the available space. The size cannot be changed by the user or programmatically. \value ResizeToContents QHeaderView will automatically resize the section to its optimal size based on the contents of the entire column or row. The size cannot be changed by the user or programmatically. (This value was introduced in 4.2) The following values are obsolete: \value Custom Use Fixed instead. \sa setResizeMode() stretchLastSection minimumSectionSize*//*! \fn void QHeaderView::sectionMoved(int logicalIndex, int oldVisualIndex, int newVisualIndex) This signal is emitted when a section is moved. The section's logical index is specified by \a logicalIndex, the old index by \a oldVisualIndex, and the new index position by \a newVisualIndex. \sa moveSection()*//*! \fn void QHeaderView::sectionResized(int logicalIndex, int oldSize, int newSize) This signal is emitted when a section is resized. The section's logical number is specified by \a logicalIndex, the old size by \a oldSize, and the new size by \a newSize. \sa resizeSection()*//*! \fn void QHeaderView::sectionPressed(int logicalIndex) This signal is emitted when a section is pressed. The section's logical index is specified by \a logicalIndex. \sa setClickable()*//*! \fn void QHeaderView::sectionClicked(int logicalIndex) This signal is emitted when a section is clicked. The section's logical index is specified by \a logicalIndex. Note that the sectionPressed signal will also be emitted. \sa setClickable(), sectionPressed()*//*! \fn void QHeaderView::sectionEntered(int logicalIndex) \since 4.3 This signal is emitted when the cursor moves over the section and the left mouse button is pressed. The section's logical index is specified by \a logicalIndex. \sa setClickable(), sectionPressed()*//*! \fn void QHeaderView::sectionDoubleClicked(int logicalIndex) This signal is emitted when a section is double-clicked. The section's logical index is specified by \a logicalIndex. \sa setClickable()*//*! \fn void QHeaderView::sectionCountChanged(int oldCount, int newCount) This signal is emitted when the number of sections changes; i.e. when sections are added or deleted. The original count is specified by \a oldCount, and the new count by \a newCount. \sa count(), length(), headerDataChanged()*//*! \fn void QHeaderView::sectionHandleDoubleClicked(int logicalIndex) This signal is emitted when a section is double-clicked. The section's logical index is specified by \a logicalIndex. \sa setClickable()*//*! \fn void QHeaderView::sortIndicatorChanged(int logicalIndex, Qt::SortOrder order) \since 4.3 This signal is emitted when the section containing the sort indicator or the order indicated is changed. The section's logical index is specified by \a logicalIndex and the sort order is specified by \a order. \sa setSortIndicator()*//*! \fn void QHeaderView::sectionAutoResize(int logicalIndex, QHeaderView::ResizeMode mode) This signal is emitted when a section is automatically resized. The section's logical index is specified by \a logicalIndex, and the resize mode by \a mode. \sa setResizeMode(), stretchLastSection()*/// ### Qt 5: change to sectionAutoResized()/*! \fn void QHeaderView::geometriesChanged() \since 4.2 This signal is emitted when the header geometries has changed.*//*! \property QHeaderView::highlightSections \brief whether the sections containing selected items are highlighted*//*! Creates a new generic header with the given \a orientation and \a parent.*/QHeaderView::QHeaderView(Qt::Orientation orientation, QWidget *parent) : QAbstractItemView(*new QHeaderViewPrivate, parent){ Q_D(QHeaderView); d->setDefaultValues(orientation); initialize();}/*! \internal*/QHeaderView::QHeaderView(QHeaderViewPrivate &dd, Qt::Orientation orientation, QWidget *parent) : QAbstractItemView(dd, parent){ Q_D(QHeaderView); d->setDefaultValues(orientation); initialize();}/*! Destroys the header.*/QHeaderView::~QHeaderView(){}/*! \internal*/void QHeaderView::initialize(){ Q_D(QHeaderView); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setFrameStyle(NoFrame); setFocusPolicy(Qt::NoFocus); d->viewport->setMouseTracking(true); d->viewport->setBackgroundRole(QPalette::Button); d->textElideMode = Qt::ElideNone; delete d->itemDelegate;}/*! \reimp*/void QHeaderView::setModel(QAbstractItemModel *model){ if (model == this->model()) return; Q_D(QHeaderView); if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) { if (d->orientation == Qt::Horizontal) { QObject::disconnect(d->model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int))); QObject::disconnect(d->model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int))); QObject::disconnect(d->model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(_q_sectionsRemoved(QModelIndex,int,int))); } else { QObject::disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int))); QObject::disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int))); QObject::disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_sectionsRemoved(QModelIndex,int,int))); } QObject::disconnect(d->model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(headerDataChanged(Qt::Orientation,int,int))); QObject::disconnect(d->model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); } if (model && model != QAbstractItemModelPrivate::staticEmptyModel()) { if (d->orientation == Qt::Horizontal) { QObject::connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int))); QObject::connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int))); QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), this, SLOT(_q_sectionsRemoved(QModelIndex,int,int))); } else { QObject::connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(sectionsInserted(QModelIndex,int,int))); QObject::connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), this, SLOT(sectionsAboutToBeRemoved(QModelIndex,int,int))); QObject::connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_sectionsRemoved(QModelIndex,int,int))); } QObject::connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)), this, SLOT(headerDataChanged(Qt::Orientation,int,int))); QObject::connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(_q_layoutAboutToBeChanged())); } d->state = QHeaderViewPrivate::NoClear; QAbstractItemView::setModel(model); d->state = QHeaderViewPrivate::NoState; // Users want to set sizes and modes before the widget is shown. // Thus, we have to initialize when the model is set, // and not lazily like we do in the other views. initializeSections();}/*! Returns the orientation of the header. \sa Qt::Orientation*/Qt::Orientation QHeaderView::orientation() const{ Q_D(const QHeaderView); return d->orientation;}/*! Returns the offset of the header: this is the header's left-most (or top-most for vertical headers) visible pixel. \sa setOffset()*/int QHeaderView::offset() const{ Q_D(const QHeaderView); return d->offset;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -