QTreeView - allow users to control data in the treestructure
This patch allows to set which logical index the tree is in. Before the tree always displayed data from the logical index 0, but it is actually more likely that the user wants to have data from visual index 0 (which can be done by special value -1). There is nothing special about logical index 0, and not being able to change the tree-data is just annoying. Change-Id: Ib070ce93343a0d2fbac3ad5a42cb4359401ac87c Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
parent
b215176da3
commit
658e42e77a
3
dist/changes-5.2.0
vendored
3
dist/changes-5.2.0
vendored
@ -26,6 +26,9 @@ QtWidgets
|
||||
it will make use of the new protected viewportSizeHint() (binary compatible since it
|
||||
was reserved in Qt5). This function returns a suggested size based on contents.
|
||||
|
||||
- QTreeView now has setTreePosition to allow the treestructure to show data from other
|
||||
columns than logicalindex zero.
|
||||
|
||||
- [QTBUG-4206] QTableView resizeToContents will now adjust to actual contents
|
||||
and not just visible area. QHeaderView::setAutoResizePrecision() has been
|
||||
introduced to control how precise the autoResize should be.
|
||||
|
@ -953,6 +953,36 @@ bool QTreeView::wordWrap() const
|
||||
return d->wrapItemText;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.2
|
||||
|
||||
This specifies that the tree structure should be placed at logical index \a index.
|
||||
If \index is set to -1 then the tree will always follow visual index 0.
|
||||
|
||||
\sa treePosition(), QHeaderView::swapSections(), QHeaderView::moveSection()
|
||||
*/
|
||||
|
||||
void QTreeView::setTreePosition(int index)
|
||||
{
|
||||
Q_D(QTreeView);
|
||||
d->treePosition = index;
|
||||
update();
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 5.2
|
||||
|
||||
Return the logical index the tree is set on. If the return value is -1 then the
|
||||
tree is placed on the visual index 0.
|
||||
|
||||
\sa setTreePosition()
|
||||
*/
|
||||
|
||||
int QTreeView::treePosition() const
|
||||
{
|
||||
Q_D(const QTreeView);
|
||||
return d->treePosition;
|
||||
}
|
||||
|
||||
/*!
|
||||
\reimp
|
||||
@ -1068,7 +1098,7 @@ QRect QTreeView::visualRect(const QModelIndex &index) const
|
||||
int x = (spanning ? 0 : columnViewportPosition(index.column()));
|
||||
int w = (spanning ? d->header->length() : columnWidth(index.column()));
|
||||
// handle indentation
|
||||
if (index.column() == 0) {
|
||||
if (d->isTreePosition(index.column())) {
|
||||
int i = d->indentationForItem(vi);
|
||||
w -= i;
|
||||
if (!isRightToLeft())
|
||||
@ -1285,6 +1315,14 @@ void QTreeView::paintEvent(QPaintEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
int QTreeViewPrivate::logicalIndexForTree() const
|
||||
{
|
||||
int index = treePosition;
|
||||
if (index < 0)
|
||||
index = header->logicalIndex(0);
|
||||
return index;
|
||||
}
|
||||
|
||||
void QTreeViewPrivate::paintAlternatingRowColors(QPainter *painter, QStyleOptionViewItem *option, int y, int bottom) const
|
||||
{
|
||||
Q_Q(const QTreeView);
|
||||
@ -1517,7 +1555,7 @@ void QTreeViewPrivate::calcLogicalIndices(QVector<int> *logicalIndices, QVector<
|
||||
if (columnCount == 1 || (nextLogicalSection == 0 && prevLogicalSection == -1)
|
||||
|| (headerSection == 0 && nextLogicalSection == -1) || spanning)
|
||||
pos = QStyleOptionViewItem::OnlyOne;
|
||||
else if (headerSection == 0 || (nextLogicalSection != 0 && prevLogicalSection == -1))
|
||||
else if (isTreePosition(headerSection) || (nextLogicalSection != 0 && prevLogicalSection == -1))
|
||||
pos = QStyleOptionViewItem::Beginning;
|
||||
else if (nextLogicalSection == 0 || nextLogicalSection == -1)
|
||||
pos = QStyleOptionViewItem::End;
|
||||
@ -1541,7 +1579,7 @@ int QTreeViewPrivate::widthHintForIndex(const QModelIndex &index, int hint, cons
|
||||
hint = qBound(min, hint, max);
|
||||
}
|
||||
int xhint = delegateForIndex(index)->sizeHint(option, index).width();
|
||||
hint = qMax(hint, xhint + (index.column() == 0 ? indentationForItem(i) : 0));
|
||||
hint = qMax(hint, xhint + (isTreePosition(index.column()) ? indentationForItem(i) : 0));
|
||||
return hint;
|
||||
}
|
||||
|
||||
@ -1686,7 +1724,7 @@ void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
alternate row color was provided by the view. For backward compatibility,
|
||||
this is now delegated to the style using PE_PanelViewItemRow which
|
||||
does the appropriate fill */
|
||||
if (headerSection == 0) {
|
||||
if (d->isTreePosition(headerSection)) {
|
||||
const int i = d->indentationForItem(d->current);
|
||||
QRect branches(reverse ? position + width - i : position, y, i, height);
|
||||
const bool setClipRect = branches.width() > width;
|
||||
@ -3663,7 +3701,7 @@ int QTreeViewPrivate::itemDecorationAt(const QPoint &pos) const
|
||||
executePostedLayout();
|
||||
int x = pos.x();
|
||||
int column = header->logicalIndexAt(x);
|
||||
if (column != 0)
|
||||
if (!isTreePosition(column))
|
||||
return -1; // no logical index at x
|
||||
|
||||
int viewItemIndex = itemAtCoordinate(pos.y());
|
||||
@ -3685,8 +3723,8 @@ QRect QTreeViewPrivate::itemDecorationRect(const QModelIndex &index) const
|
||||
return QRect();
|
||||
|
||||
int itemIndentation = indentationForItem(viewItemIndex);
|
||||
int position = header->sectionViewportPosition(0);
|
||||
int size = header->sectionSize(0);
|
||||
int position = header->sectionViewportPosition(logicalIndexForTree());
|
||||
int size = header->sectionSize(logicalIndexForTree());
|
||||
|
||||
QRect rect;
|
||||
if (q->isRightToLeft())
|
||||
|
@ -128,6 +128,9 @@ public:
|
||||
void setWordWrap(bool on);
|
||||
bool wordWrap() const;
|
||||
|
||||
void setTreePosition(int logicalIndex);
|
||||
int treePosition() const;
|
||||
|
||||
void keyboardSearch(const QString &search);
|
||||
|
||||
QRect visualRect(const QModelIndex &index) const;
|
||||
|
@ -91,10 +91,16 @@ public:
|
||||
expandsOnDoubleClick(true),
|
||||
allColumnsShowFocus(false), current(0), spanning(false),
|
||||
animationsEnabled(false), columnResizeTimerID(0),
|
||||
autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false) {}
|
||||
autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false),
|
||||
treePosition(0) {}
|
||||
|
||||
~QTreeViewPrivate() {}
|
||||
void initialize();
|
||||
int logicalIndexForTree() const;
|
||||
inline bool isTreePosition(int logicalIndex) const
|
||||
{
|
||||
return logicalIndex == logicalIndexForTree();
|
||||
}
|
||||
|
||||
QItemViewPaintPairs draggablePaintPairs(const QModelIndexList &indexes, QRect *r) const;
|
||||
void adjustViewOptionsForIndex(QStyleOptionViewItem *option, const QModelIndex ¤t) const;
|
||||
@ -252,6 +258,9 @@ public:
|
||||
|
||||
// If we should clean the set
|
||||
bool hasRemovedItems;
|
||||
|
||||
// tree position
|
||||
int treePosition;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -1,2 +1,2 @@
|
||||
TEMPLATE = subdirs
|
||||
SUBDIRS = delegate qheaderview qtreeview
|
||||
SUBDIRS = delegate qheaderview qtreeview qtreewidget
|
||||
|
152
tests/manual/widgets/itemviews/qtreewidget/main.cpp
Normal file
152
tests/manual/widgets/itemviews/qtreewidget/main.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2012 Thorbjørn Lund Martsum - tmartsum[at]gmail.com
|
||||
** Contact: http://www.qt-project.org/legal
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:LGPL$
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Digia. For licensing terms and
|
||||
** conditions see http://qt.digia.com/licensing. For further information
|
||||
** use the contact form at http://qt.digia.com/contact-us.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** In addition, as a special exception, Digia gives you certain additional
|
||||
** rights. These rights are described in the Digia Qt LGPL Exception
|
||||
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be
|
||||
** met: http://www.gnu.org/copyleft/gpl.html.
|
||||
**
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <QVBoxLayout>
|
||||
#include <QTreeWidget>
|
||||
#include <QGroupBox>
|
||||
#include <QRadioButton>
|
||||
#include <QDialog>
|
||||
#include <QApplication>
|
||||
#include <QHeaderView>
|
||||
|
||||
class ExampleDlg : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QVBoxLayout *groupLayout;
|
||||
QVBoxLayout *dialogLayout;
|
||||
QTreeWidget *treeWidget;
|
||||
QGroupBox *groupBox;
|
||||
QRadioButton *radioFirstName;
|
||||
QRadioButton *radioLastName;
|
||||
QRadioButton *radioDeveloperNo;
|
||||
QRadioButton *radioTitle;
|
||||
|
||||
ExampleDlg() : QDialog(0)
|
||||
{
|
||||
dialogLayout = new QVBoxLayout(this);
|
||||
treeWidget = new QTreeWidget(this);
|
||||
dialogLayout->addWidget(treeWidget);
|
||||
|
||||
groupBox = new QGroupBox(this);
|
||||
groupLayout = new QVBoxLayout(groupBox);
|
||||
radioFirstName = new QRadioButton("First Name", groupBox);
|
||||
groupLayout->addWidget(radioFirstName);
|
||||
radioLastName = new QRadioButton("Last Name", groupBox);
|
||||
groupLayout->addWidget(radioLastName);
|
||||
radioDeveloperNo = new QRadioButton("Developer No.", groupBox);
|
||||
groupLayout->addWidget(radioDeveloperNo);
|
||||
radioTitle = new QRadioButton("Title", groupBox);
|
||||
groupLayout->addWidget(radioTitle);
|
||||
dialogLayout->addWidget(groupBox);
|
||||
|
||||
QStringList item1sl("Barry");
|
||||
item1sl.append("Butter");
|
||||
item1sl.append("12199");
|
||||
item1sl.append("Key Maintainer");
|
||||
QTreeWidgetItem *item1 = new QTreeWidgetItem(treeWidget, item1sl);
|
||||
|
||||
QStringList item2sl("Cordon");
|
||||
item2sl.append("Rampsey");
|
||||
item2sl.append("59299");
|
||||
item2sl.append("Maintainer");
|
||||
QTreeWidgetItem *item2 = new QTreeWidgetItem(item1, item2sl);
|
||||
|
||||
QStringList item3sl("Samuel le");
|
||||
item3sl.append("Smackson");
|
||||
item3sl.append("708");
|
||||
item3sl.append("Contributer");
|
||||
/* QTreeWidgetItem *item3 = */ new QTreeWidgetItem(item2, item3sl);
|
||||
|
||||
QStringList item4sl("Georg");
|
||||
item4sl.append("Ambush");
|
||||
item4sl.append("86999");
|
||||
item4sl.append("Area Maintainer");
|
||||
QTreeWidgetItem *item4 = new QTreeWidgetItem(item1, item4sl);
|
||||
|
||||
QStringList item5sl("Arne");
|
||||
item5sl.append("Strassenleger");
|
||||
item5sl.append("338999");
|
||||
item5sl.append("Approver");
|
||||
/* QTreeWidgetItem *item4 =*/ new QTreeWidgetItem(item4, item5sl);
|
||||
|
||||
treeWidget->setColumnCount(item2sl.size());
|
||||
QStringList itemInfo("First Name");
|
||||
itemInfo.append("Last Name");
|
||||
itemInfo.append("Developer No.");
|
||||
// Developer no. could also have been social security number og some other id.
|
||||
itemInfo.append("Title");
|
||||
treeWidget->setHeaderLabels(itemInfo);
|
||||
radioFirstName->setChecked(true);
|
||||
|
||||
connect(radioFirstName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool)));
|
||||
connect(radioLastName, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool)));
|
||||
connect(radioDeveloperNo, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool)));
|
||||
connect(radioTitle, SIGNAL(toggled(bool)), this, SLOT(fixDataInTree(bool)));
|
||||
treeWidget->setTreePosition(-1);
|
||||
}
|
||||
|
||||
protected slots:
|
||||
void fixDataInTree(bool checked)
|
||||
{
|
||||
if (!checked)
|
||||
return;
|
||||
int colInTree = 0; // first Name
|
||||
if (radioLastName->isChecked())
|
||||
colInTree = 1;
|
||||
if (radioDeveloperNo->isChecked())
|
||||
colInTree = 2;
|
||||
if (radioTitle->isChecked())
|
||||
colInTree = 3;
|
||||
treeWidget->header()->swapSections(0, treeWidget->header()->visualIndex(colInTree));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
ExampleDlg d;
|
||||
d.show();
|
||||
app.exec();
|
||||
}
|
||||
|
||||
#include "main.moc"
|
@ -0,0 +1,4 @@
|
||||
TEMPLATE = app
|
||||
SOURCES = main.cpp
|
||||
QT += widgets core-private
|
||||
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
|
Loading…
x
Reference in New Issue
Block a user