summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/widgets/itemviews/qheaderview.cpp50
-rw-r--r--src/widgets/itemviews/qheaderview.h3
-rw-r--r--src/widgets/itemviews/qheaderview_p.h2
-rw-r--r--tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp14
4 files changed, 66 insertions, 3 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index 75cd07bb80..239cc84a0a 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -81,6 +81,7 @@ QDataStream &operator>>(QDataStream &in, QHeaderViewPrivate::SectionItem &sectio
#endif // QT_NO_DATASTREAM
static const int maxSizeSection = 1048575; // since section size is in a bitfield (uint 20). See qheaderview_p.h
+ // if this is changed then the docs in maximumSectionSize should be changed.
/*!
\class QHeaderView
@@ -569,7 +570,7 @@ void QHeaderView::setVisible(bool v)
/*!
Returns a suitable size hint for the section specified by \a logicalIndex.
- \sa sizeHint(), defaultSectionSize(), minimumSectionSize(),
+ \sa sizeHint(), defaultSectionSize(), minimumSectionSize(), maximumSectionSize()
Qt::SizeHintRole
*/
@@ -587,7 +588,7 @@ int QHeaderView::sectionSizeHint(int logicalIndex) const
else
size = sectionSizeFromContents(logicalIndex);
int hint = d->orientation == Qt::Horizontal ? size.width() : size.height();
- return qMax(minimumSectionSize(), hint);
+ return qBound(minimumSectionSize(), hint, maximumSectionSize());
}
/*!
@@ -1606,9 +1607,49 @@ void QHeaderView::setMinimumSectionSize(int size)
if (size < 0 || size > maxSizeSection)
return;
d->minimumSectionSize = size;
+ if (d->minimumSectionSize > maximumSectionSize())
+ d->maximumSectionSize = size;
}
/*!
+ \since 5.2
+ \property QHeaderView::maximumSectionSize
+ \brief the maximum size of the header sections.
+
+ The maximum section size is the largest section size allowed.
+ The default value for this property is 1048575, which is also the largest
+ possible size for a section. Setting maximum to -1 will reset the value to
+ the largest section size.
+
+ With exception of stretch this property is honored by all \l{ResizeMode}{resize modes}
+
+ \sa setSectionResizeMode(), defaultSectionSize
+*/
+int QHeaderView::maximumSectionSize() const
+{
+ Q_D(const QHeaderView);
+ if (d->maximumSectionSize == -1)
+ return maxSizeSection;
+ return d->maximumSectionSize;
+}
+
+void QHeaderView::setMaximumSectionSize(int size)
+{
+ Q_D(QHeaderView);
+ if (size == -1) {
+ d->maximumSectionSize = maxSizeSection;
+ return;
+ }
+ if (size < 0 || size > maxSizeSection)
+ return;
+ if (minimumSectionSize() > size)
+ d->minimumSectionSize = size;
+
+ d->maximumSectionSize = size;
+}
+
+
+/*!
\since 4.1
\property QHeaderView::defaultAlignment
\brief the default alignment of the text in each header section
@@ -2417,7 +2458,8 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
d->cascadingResize(visual, d->headerSectionSize(visual) + delta);
} else {
int delta = d->reverse() ? d->firstPos - pos : pos - d->firstPos;
- resizeSection(d->section, qMax(d->originalSize + delta, minimumSectionSize()));
+ int newsize = qBound(minimumSectionSize(), d->originalSize + delta, maximumSectionSize());
+ resizeSection(d->section, newsize);
}
d->lastPos = pos;
return;
@@ -3236,6 +3278,8 @@ void QHeaderViewPrivate::resizeSections(QHeaderView::ResizeMode globalMode, bool
int logicalIndex = q->logicalIndex(i);
sectionSize = qMax(viewSectionSizeHint(logicalIndex),
q->sectionSizeHint(logicalIndex));
+ if (sectionSize > q->maximumSectionSize())
+ sectionSize = q->maximumSectionSize();
}
section_sizes.append(sectionSize);
lengthToStretch -= sectionSize;
diff --git a/src/widgets/itemviews/qheaderview.h b/src/widgets/itemviews/qheaderview.h
index 3de3d320bf..b3ed666aa6 100644
--- a/src/widgets/itemviews/qheaderview.h
+++ b/src/widgets/itemviews/qheaderview.h
@@ -61,6 +61,7 @@ class Q_WIDGETS_EXPORT QHeaderView : public QAbstractItemView
Q_PROPERTY(bool cascadingSectionResizes READ cascadingSectionResizes WRITE setCascadingSectionResizes)
Q_PROPERTY(int defaultSectionSize READ defaultSectionSize WRITE setDefaultSectionSize)
Q_PROPERTY(int minimumSectionSize READ minimumSectionSize WRITE setMinimumSectionSize)
+ Q_PROPERTY(int maximumSectionSize READ maximumSectionSize WRITE setMaximumSectionSize)
Q_PROPERTY(Qt::Alignment defaultAlignment READ defaultAlignment WRITE setDefaultAlignment)
Q_ENUMS(ResizeMode)
@@ -166,6 +167,8 @@ public:
int minimumSectionSize() const;
void setMinimumSectionSize(int size);
+ int maximumSectionSize() const;
+ void setMaximumSectionSize(int size);
Qt::Alignment defaultAlignment() const;
void setDefaultAlignment(Qt::Alignment alignment);
diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h
index a8c620b9d2..b2af821e9b 100644
--- a/src/widgets/itemviews/qheaderview_p.h
+++ b/src/widgets/itemviews/qheaderview_p.h
@@ -95,6 +95,7 @@ public:
stretchSections(0),
contentsSections(0),
minimumSectionSize(-1),
+ maximumSectionSize(-1),
lastSectionSize(0),
sectionIndicatorOffset(0),
sectionIndicator(0),
@@ -287,6 +288,7 @@ public:
int contentsSections;
int defaultSectionSize;
int minimumSectionSize;
+ int maximumSectionSize;
int lastSectionSize; // $$$
int sectionIndicatorOffset;
Qt::Alignment defaultAlignment;
diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
index 09f0161dff..398b1b4d98 100644
--- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
+++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
@@ -2721,6 +2721,20 @@ void tst_QHeaderView::resizeToContentTest()
QVERIFY(view->sectionSize(1) > 1);
QVERIFY(view->sectionSize(2) > 1);
+ // Check minimum section size
+ hh->setMinimumSectionSize(150);
+ model->setData(idx, QVariant("i"));
+ hh->resizeSections(QHeaderView::ResizeToContents);
+ QCOMPARE(hh->sectionSize(3), 150);
+ hh->setMinimumSectionSize(-1);
+
+ // Check maximumSection size
+ hh->setMaximumSectionSize(200);
+ model->setData(idx, QVariant("This is a even longer string that is expected to be more than 200 pixels"));
+ hh->resizeSections(QHeaderView::ResizeToContents);
+ QCOMPARE(hh->sectionSize(3), 200);
+ hh->setMaximumSectionSize(-1);
+
view->setDefaultSectionSize(25); // To make sure our precalced data are correct. We do not know font height etc.
const int precalced_results[] = { -1523279360, -1523279360, -1347156568, 1, 1719705216, 1719705216, 12500 };