ロボットを制御するための GUI を開発中です。そのロボットに関する現在の状態データを示すテーブルがあります。そして、テーブルはテスト サーバー内のデータから定期的に更新されます。更新が接続されていない場合、テーブル内の行を選択して、選択した行のデータを保存するのは簡単でした。ただし、サーバーが接続されていると、行を選択できなくなります(行をクリックすると、選択した行が一瞬だけ強調表示されて消えます)。印刷方法を使用して、何が起こったのかを確認しようとしました内部では、更新が実行されているときにスロットが実装されないようです.また、更新も頻繁に行の選択を解除します.行を手動で選択すると、更新をブロックし、ピースで同じ行を自動的に選択することを検討していますのロジックと selectRow() メソッド。
1. 入力を管理するために、Gui Controller クラスのスロットとして機能する update メソッド
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