QTableView - 滚动时更改选择

发布于 2024-12-22 18:59:51 字数 211 浏览 1 评论 0原文

我有一个 QTableView。我希望滚动时移动选择 - 因此光标始终可见。

在此处输入图像描述

QTableView.selectRow(rowNo),但是您有建议吗在哪里调用这个?

理想情况下,我希望在滚动所选行时位于中心。

I have a QTableView. I want the selection to be moved when i scroll - so the cursor would be always visible.

enter image description here

There is QTableView.selectRow(rowNo), but do you have a suggestion where to call this?

Ideally i would like upon scrolling the selected row to be in the center.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-12-29 18:59:52

您可以使用方法 .indexAt(viewport().pos())。您可能需要稍微调整一下位置。即按标题大小移动它。当你有索引时,你可以简单地调用 .row() 方法

You could use method .indexAt(viewport().pos()). You may need to fix position a little bit. i.e. move it by headers size. When you have index you can simply call .row() method

疯狂的代价 2024-12-29 18:59:52

我这样做了(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)

I did it like this (PyQt4):

Upon init i connect to scrollbar event:

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

Then in the handler:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文