2013-07-08 34 views
4

的Qt 4.8QTreeView則:: scrollTo不工作

我有一個QTreeView基於類與asociated QAbstractItemModel基於類。如果我用新信息重新加載模型,我想展開/滾動樹到上一個選定的項目。

使用QTreeView::setSelectionModel(...)正確地創建和連接了兩個類,樹視圖和模型。

重裝模型後,我得到一個有效的索引到以前選擇的項目,我scrollTo它:

myTreeView->scrollTo(index); 

但樹沒有展開。但是,如果我手動展開樹,則會真正選擇該項目。

樹視圖中初始化contruct有:

header()->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel); 
header()->setStretchLastSection(false); 
header()->setResizeMode(0, QHeaderView::ResizeToContents); 

關於擴大樹選擇任何想法​​?

+0

它是一種錯誤和沒有得到固定在Qt 4.x的,https://bugreports.qt.io/browse/QTBUG-9326 – NDestiny

回答

0

QTreeView::scrollTo應該適當地擴展層次結構。

當模型更新時(也許仍然選擇正確的行,因爲行信息仍然有效,儘管父系不是,請不要問我這些內部工作是如何工作的),很可能會使對象失效。從QModelIndexdocumentation

注意:模型索引應立即使用,然後丟棄。在調用改變模型結構或刪除項目的模型函數之後,您不應該依賴索引來保持有效。如果您需要隨時間保留模型索引,請使用QPersistentModelIndex

你當然可以查看到QPersistentModelIndex對象,但像它說在它的documentation

這是很好的做法,以檢查持久化模型索引使用它們之前有效。

否則,您可以隨時在模型刷新後再次查詢該項目。

+0

是,如果這個問題解決了不錯,但我創建了一個模型索引在基於「...的第四個孩子的第一個孩子」純數據的現有模型中,並且它仍然不起作用 – IceFire

1

即使QTreeView::scrollTo文件說:

Scroll the contents of the tree view until the given model item index is 
visible. The hint parameter specifies more precisely where the item should 
be located after the operation. If any of the parents of the model item 
are collapsed, they will be expanded to ensure that the model item is visible. 

這是不是真的(我認爲)

如果解決了手動展開所有以前的樹層次的問題:

// This slot is invoqued from model using last selected item 
void MyTreeWidget::ItemSelectedManually(const QModelIndex & ar_index) 
{ 
    std::vector<std::pair<int, int> > indexes; 

    // first of all, I save all item "offsets" relative to its parent 

    QModelIndex indexAbobe = ar_index.parent(); 
    while (indexAbobe.isValid()) 
    { 
     indexes.push_back(std::make_pair(indexAbobe.row(), indexAbobe.column())); 
     indexAbobe = indexAbobe.parent(); 
    } 

    // now, select actual selection model 

    auto model = _viewer.selectionModel()->model(); 

    // get root item 

    QModelIndex index = model->index(0, 0, QModelIndex()); 
    if (index.isValid()) 
    { 
     // now, expand all items below 

     for (auto it = indexes.rbegin(); it != indexes.rend() && index.isValid(); ++it) 
     { 
      auto row = (*it).first; 
      auto colum = (*it).second; 

      _viewer.setExpanded(index, true); 

      // and get a new item relative to parent 
      index = model->index(row, colum, index); 
     } 
    } 

    // finally, scroll to real item, after expanding everything above. 
    _viewer.scrollTo(ar_index); 
} 
2

我只是處理在類似的情況下,通過setModelIndex(它在內部以scrollTo結束)設置模型索引對我的一個模型工作正常,但對其他o東北。

當我強制展開所有1級項目時,scrollTo的工作方式與上面描述的一樣(只需撥打expandAll即可)。

的原因是在我的模型類中的錯誤:

QModelIndex MyBadModel::parent(const QModelIndex& index) const 

和我固定的,事情變得正常那裏了。錯誤是這樣的:父模型索引的internalId與「從其他方向」計算相同的模型索引(對於父項)不同,因此在該模型索引(由parent方法返回)在列表中找不到的視覺指數。

+0

@ tio-pepe應該接受這個答案,因爲同樣的問題發生在我身上。 * parent *實現中的錯誤會導致QTreeView :: scrollTo失敗。 – McLeary

0

一切都很簡單。 只需將autoExpandDelay屬性從-1更改爲0(例如)。

ui->treeView->setAutoExpandDelay(0);