简体   繁体   English

QTableView:防止用户离开特定行

[英]QTableView: Prevent a user from navigating away from a spesific row

I am having trouble preventing the user form changing the current selection, if the save action did not complete successfully. 如果保存操作未成功完成,则无法阻止用户窗体更改当前选择。 I can re-select a row using the QTableView's selection models' currentRowChanged signal but although the selection change, the blue selection indicator does not. 我可以使用QTableView的选择模型的currentRowChanged信号重新选择一行,但是尽管选择发生了变化,但是蓝色选择指示器却没有。 See the image below. 参见下图。

Example: In the image below the user attempted to add a new row nr 537. But the save action got an error and I don't want the user to navigate away from row 537 before the record is either deleted or edited and then saved 示例:在下面的图像中,用户尝试添加新行nr537。但是保存操作出现错误,我不希望用户在删除记录或进行编辑然后保存记录之前先离开537行。

Question: How do I move the blue line to the current selection? 问题:如何将蓝线移至当前选择? (the current selection is the last row) (The QTableView's Selection Behavior is set to select rows) (当前选择是最后一行)(QTableView的选择行为设置为选择行)

在此处输入图片说明

Here is the code I got so far: 这是到目前为止我得到的代码:

    def __init__(self, parent):
        ...
        self.__tableViewSelectionModel = self.__ui.tableView.selectionModel()
        self.__tableViewSelectionModel.currentRowChanged.connect(self.rowChanged)

    def rowChanged(self, current=None, previous=None):
        if save() == True:
            self.__ui.tableView.clearSelection()
            self.__ui.tableView.selectRow(previous.row())

Replacing this: 替换为:

self.__ui.tableView.clearSelection()
self.__ui.tableView.selectRow(previous.row())

with this: 有了这个:

QtCore.QTimer.singleShot(0.00001, lambda: self.__ui.tableView.selectRow(previous.row()))

produced the desired outcome 产生了预期的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM