Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不断更新的表中选择行?_Python_Pyqt_Pyqt4_Qtableview - Fatal编程技术网

Python 如何在不断更新的表中选择行?

Python 如何在不断更新的表中选择行?,python,pyqt,pyqt4,qtableview,Python,Pyqt,Pyqt4,Qtableview,伙计们,我正在做一个GUI来控制一些机器人。 有一个表显示该机器人的当前状态数据。该表会根据测试服务器中的数据定期更新。当更新未连接时,可以很容易地在表中选择一行并保存所选行的数据。但是,当服务器连接时,无法选择行(当您单击该行时,所选行将仅在一瞬间高亮显示并消失)。我尝试使用一些打印方法来查看内部发生了什么,更新运行时插槽似乎不会实现。更新也会频繁取消选择行。我正在考虑在手动选择行时阻止更新,然后使用一段逻辑和selectRow()自动选择同一行方法。你有什么建议如何阻止更新或其他方法来处理这

伙计们,我正在做一个GUI来控制一些机器人。 有一个表显示该机器人的当前状态数据。该表会根据测试服务器中的数据定期更新。当更新未连接时,可以很容易地在表中选择一行并保存所选行的数据。但是,当服务器连接时,无法选择行(当您单击该行时,所选行将仅在一瞬间高亮显示并消失)。我尝试使用一些打印方法来查看内部发生了什么,更新运行时插槽似乎不会实现。更新也会频繁取消选择行。我正在考虑在手动选择行时阻止更新,然后使用一段逻辑和selectRow()自动选择同一行方法。你有什么建议如何阻止更新或其他方法来处理这个问题吗

1.更新方法,该方法作为Gui控制器类中的插槽来管理输入

def tbRobotChanged(self, currentCell):

    # get the selected cell's index from currentCell,Implement the Slot method
    tablemodel= currentCell.model() 
    #get the model of the currentcell
    firstColumn = tablemodel.index(currentCell.row(),0)
    #change it to the first column in the row with the robot name

    self.statusBar().showMessage("Selected Robot is " +
                                 firstColumn.data().toString())
    self.selected_robot = int(firstColumn.data().toString())
    # show the selected robot name in the statusbar
2.表定义,这是定义主窗口的Gui类的一部分:

def initRobotsFrame(self, rf):
    hbox = QtGui.QHBoxLayout()
    self.robot_table = QtGui.QTableView()
    self.table_header = ['Robot', 'PosX', 'PosY', 'Heading']
    tm = RobotTableModel([[0, 0, 0, 2],[1, 1,2,4]],
                         self.table_header)
    self.robot_table.setModel(tm)
    vh = self.robot_table.verticalHeader()
    vh.setVisible(False)
    self.robot_table.resizeColumnsToContents()
    self.robot_table.setSizePolicy(QtGui.QSizePolicy(
        QtGui.QSizePolicy.Minimum,
        QtGui.QSizePolicy.Minimum))
    hbox.addWidget(self.robot_table)
    self.selected_robot = 0

    block_true=GUIController.robot_data.blockSignals(True)
    GUIController.robot_data.blockSignals(block_true)
    # select table by row instead of by cell:
    self.robot_table.setSelectionBehavior(
        QtGui.QAbstractItemView.SelectRows)        
    # set the signal and slot using selectionModel:
    self.robot_table.selectionModel().currentRowChanged.connect(
        self.tbRobotChanged) 
    self.robot_table.selectionModel().currentRowChanged.connect(
        self.updateActiveRobot)
    # implement a statusbar to test the table selection functionality:
    self.statusBar().showMessage("Ready")         
    rf.setLayout(hbox)
3.获取所选行数据的插槽:

def tbRobotChanged(self, currentCell):

    # get the selected cell's index from currentCell,Implement the Slot method
    tablemodel= currentCell.model() 
    #get the model of the currentcell
    firstColumn = tablemodel.index(currentCell.row(),0)
    #change it to the first column in the row with the robot name

    self.statusBar().showMessage("Selected Robot is " +
                                 firstColumn.data().toString())
    self.selected_robot = int(firstColumn.data().toString())
    # show the selected robot name in the statusbar 

如何更新模型?除非在每次更新时都重置模型,否则选择应该保留在视图中。@Avaris我已经找到解决方法,如果重置模型周期性运行,则会发出siganals和selectionModel(一些基本选择设置)所有的都必须循环,所以你正在重置模型。如果不是大规模更新,重置不是一个好的选择。@Avaris是的,也许我应该改变这一点,因为它是一个Gui,控制一些带有嵌入式系统的机器人,任何更快的都非常有益。谢谢你的帮助:)