2011-12-27 45 views
1

我有一個QTableView。我希望當我滾動時選擇被移動 - 所以光標將始終可見。QTableView - 滾動時更改選擇

enter image description here

QTableView.selectRow(rowNo),但你有一個建議,在哪裏打電話呢?

理想情況下,我想滾動選中的行在中心。

回答

0

我做了這樣的(PyQt4的):

在初始化我連接到滾動條事件:

self.tableView.verticalScrollBar().valueChanged.connect(self.onScroll) 

然後在處理程序:

def onScroll(self, *args): 
    "Ensure that selected row moves when scrolling - it must be always visible." 
    tableView = self.tableView 
    currentRow = tableView.selectionModel().currentIndex().row() 
    rect = tableView.viewport().rect() 
    topRow = tableView.indexAt(rect.topLeft()).row() 
    if currentRow < topRow: 
     tableView.selectRow(topRow) 
    else: 
     bottomRow = tableView.indexAt(rect.bottomLeft()).row() 
     if currentRow > bottomRow: 
      tableView.selectRow(bottomRow) 
1

您可以使用method .indexAt(viewport()。pos())。您可能需要修復一下位置。即通過標題大小移動它。當你有索引,你可以簡單地調用.row()方法