如何在连续更新表中选择一行?

时间:2013-02-19 13:21:03

标签: python pyqt pyqt4 qtableview

伙计们。我正在研究用于控制一些机器人的GUI。 有一个表显示有关该机器人的当前状态数据。该表定期从测试服务器中的数据更新。当更新未连接时,很容易在表中选择一行并保存所选行的数据。但是,当服务器连接时,就不可能选择一行(当你点击该行时,所选行将只在闪存中突出显示并且消失了。)我尝试使用一些打印方法来查看发生了什么在内部,似乎更新运行时不会实现插槽。更新也会频繁取消选择行。我正在考虑在手动选择行时阻止更新,然后自动选择同一行一块逻辑和selectRow()方法。你有任何建议如何阻止更新或其他方式来处理这个问题?

1.updating方法,作为Gui Controller类中的插槽来管理输入

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.table定义,这是定义主窗口的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.slot获取所选行的数据:

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 

0 个答案:

没有答案