QHeaderView - Fix auto-scroll on manual move on sections
authorThorbjørn Lund Martsum <tmartsum@gmail.com>
Wed, 31 Oct 2012 15:01:53 +0000 (16:01 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 23 Nov 2012 16:04:00 +0000 (17:04 +0100)
This patch fixes the manual move of sections when auto scroll is on.
It is done in QAbstractItemView::doAutoScroll by letting the
qheaderView use its parents scrollbars if they are childs of a
QTableView or QTreeView.

Task-number: QTBUG-993
Task-number: QTBUG-1103

Change-Id: I70d999d9a07c3566e42d01cc5ebb47a69a83d9d4
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
src/widgets/itemviews/qabstractitemview.cpp
src/widgets/itemviews/qheaderview.cpp
tests/manual/widgets/itemviews/qheaderview/qheaderviewtest1.cpp

index 8ba078f..57c3b44 100644 (file)
@@ -55,6 +55,9 @@
 #include <qdatetime.h>
 #include <qlineedit.h>
 #include <qspinbox.h>
+#include <qtreeview.h>
+#include <qtableview.h>
+#include <qheaderview.h>
 #include <qstyleditemdelegate.h>
 #include <private/qabstractitemview_p.h>
 #include <private/qabstractitemmodel_p.h>
@@ -3749,6 +3752,22 @@ void QAbstractItemView::doAutoScroll()
     QScrollBar *verticalScroll = verticalScrollBar();
     QScrollBar *horizontalScroll = horizontalScrollBar();
 
+    // QHeaderView does not (normally) have scrollbars
+    // It needs to use its parents scroll instead
+    QHeaderView *hv = qobject_cast<QHeaderView*>(this);
+    if (hv) {
+        QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
+        if (parent) {
+            if (hv->orientation() == Qt::Horizontal) {
+                if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
+                    horizontalScroll = parent->horizontalScrollBar();
+            } else {
+                if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
+                    verticalScroll = parent->verticalScrollBar();
+            }
+        }
+    }
+
     int verticalStep = verticalScroll->pageStep();
     int horizontalStep = horizontalScroll->pageStep();
     if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
index dc80a4b..6326b49 100644 (file)
@@ -2333,6 +2333,8 @@ void QHeaderView::mouseMoveEvent(QMouseEvent *e)
             return;
         }
         case QHeaderViewPrivate::MoveSection: {
+            if (d->shouldAutoScroll(e->pos()))
+                d->startAutoScroll();
             if (qAbs(pos - d->firstPos) >= QApplication::startDragDistance()
                 || !d->sectionIndicator->isHidden()) {
                 int visual = visualIndexAt(pos);
index 5a1db13..2fc7bbe 100644 (file)
@@ -97,14 +97,14 @@ int main(int argc, char *argv[])
     QApplication app(argc, argv);
     QTableView tv;
     QStandardItemModel m;
-    m.setRowCount(10);
-    m.setColumnCount(36);
+    m.setRowCount(500);
+    m.setColumnCount(250);
     tv.setModel(&m);
     SomeHandler handler(tv.horizontalHeader(), &tv);
-    tv.horizontalHeader()->setDefaultSectionSize(50);
+    tv.horizontalHeader()->setDefaultSectionSize(30);
+    tv.show();
     tv.horizontalHeader()->setSectionsMovable(true);
-    tv.showMaximized();
+    tv.verticalHeader()->setSectionsMovable(true);
     app.exec();
 }
-
 #include "qheaderviewtest1.moc"