--- qt-everywhere-opensource-src-4.7.3\src\gui\itemviews\qtreeview.cpp	Wed Mar 30 15:19:06 2011
+++ qt-everywhere-opensource-src-4.7.3\src\gui\itemviews\qtreeview.cpp	Wed Jun 27 15:35:38 2012
@@ -1408,6 +1408,12 @@
 
     const int viewportWidth = d->viewport->width();
 
+    // walk trough the sections in visual order to find the first visible logical section
+    int visualIndex = 0;
+    while (d->header->isSectionHidden(d->header->logicalIndex(visualIndex)) && visualIndex < d->header->count())
+      visualIndex++;
+    d->firstVisibleLogicalSection = d->header->logicalIndex(visualIndex);
+
     QVector<QRect> rects = region.rects();
     QVector<int> drawn;
     bool multipleRects = (rects.size() > 1);
@@ -1508,19 +1514,11 @@
     }
 
     const bool widgetHasFocus = hasFocus();
-    bool currentRowHasFocus = false;
-    if (allColumnsShowFocus && widgetHasFocus && current.isValid()) {
-        // check if the focus index is before or after the visible columns
-        const int r = index.row();
-        for (int c = 0; c < left && !currentRowHasFocus; ++c) {
-            QModelIndex idx = d->model->index(r, c, parent);
-            currentRowHasFocus = (idx == current);
-        }
-        QModelIndex parent = d->model->parent(index);
-        for (int c = right; c < header->count() && !currentRowHasFocus; ++c) {
-            currentRowHasFocus = (d->model->index(r, c, parent) == current);
-        }
-    }
+    bool currentRowHasFocus = allColumnsShowFocus
+                && widgetHasFocus
+                && current.isValid()
+                && current.parent() == index.parent()
+                && current.row() == index.row();
 
     // ### special case: treeviews with multiple columns draw
     // the selections differently than with only one column
@@ -1586,11 +1584,11 @@
                                  ? logicalIndexBeforeLeft
                                  : logicalIndices.at(currentLogicalSection - 1);
         if (columnCount == 1 || (nextLogicalSection == 0 && prevLogicalSection == -1)
-            || (headerSection == 0 && nextLogicalSection == -1) || spanning)
+            || (headerSection  == d->firstVisibleLogicalSection && nextLogicalSection == -1) || spanning)
             opt.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
-        else if (headerSection == 0 || (nextLogicalSection != 0 && prevLogicalSection == -1))
+        else if (headerSection == d->firstVisibleLogicalSection || (nextLogicalSection != 0 && prevLogicalSection == -1))
             opt.viewItemPosition = QStyleOptionViewItemV4::Beginning;
-        else if (nextLogicalSection == 0 || nextLogicalSection == -1)
+        else if (nextLogicalSection == d->firstVisibleLogicalSection || nextLogicalSection == -1)
             opt.viewItemPosition = QStyleOptionViewItemV4::End;
         else
             opt.viewItemPosition = QStyleOptionViewItemV4::Middle;
@@ -1638,7 +1636,7 @@
            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 (headerSection == d->firstVisibleLogicalSection) {
             const int i = d->indentationForItem(d->current);
             QRect branches(reverse ? position + width - i : position, y, i, height);
             const bool setClipRect = branches.width() > width;
@@ -1872,7 +1870,7 @@
         d->executePostedLayout(); // we need to make sure viewItems is updated
         if (d->itemsExpandable
             && d->expandsOnDoubleClick
-            && d->hasVisibleChildren(persistent)) {
+            && d->hasVisibleChildren(persistent.sibling(persistent.row(), 0))) {
             if (!((i < d->viewItems.count()) && (d->viewItems.at(i).index == firstColumnIndex))) {
                 // find the new index of the item
                 for (i = 0; i < d->viewItems.count(); ++i) {
@@ -3506,7 +3504,7 @@
     executePostedLayout();
     int x = pos.x();
     int column = header->logicalIndexAt(x);
-    if (column != 0)
+    if (column != firstVisibleLogicalSection)
         return -1; // no logical index at x
 
     int viewItemIndex = itemAtCoordinate(pos.y());
@@ -3528,8 +3526,8 @@
         return QRect();
 
     int itemIndentation = indentationForItem(viewItemIndex);
-    int position = header->sectionViewportPosition(0);
-    int size = header->sectionSize(0);
+    int position = header->sectionViewportPosition(firstVisibleLogicalSection);
+    int size = header->sectionSize(firstVisibleLogicalSection);
 
     QRect rect;
     if (q->isRightToLeft())
--- qt-everywhere-opensource-src-4.7.3\src\gui\itemviews\qtreeview_p.h	Wed Mar 30 15:19:06 2011
+++ qt-everywhere-opensource-src-4.7.3\src\gui\itemviews\qtreeview_p.h	Wed Jun 27 15:34:57 2012
@@ -89,7 +89,7 @@
           uniformRowHeights(false), rootDecoration(true),
           itemsExpandable(true), sortingEnabled(false),
           expandsOnDoubleClick(true),
-          allColumnsShowFocus(false), current(0), spanning(false),
+          allColumnsShowFocus(false), current(0), spanning(false), firstVisibleLogicalSection(0),
           animationsEnabled(false), columnResizeTimerID(0),
           autoExpandDelay(-1), hoverBranch(-1), geometryRecursionBlock(false), hasRemovedItems(false) {}
 
@@ -184,6 +184,7 @@
     mutable QPair<int,int> leftAndRight;
     mutable int current;
     mutable bool spanning;
+    mutable int firstVisibleLogicalSection;
 
     // used when expanding and collapsing items
     QSet<QPersistentModelIndex> expandedIndexes;
