4

と:

tableView = QTableView()
rows = [0, 1, 2]

tableView.selectRow(0)または、他のすべての選択を解除して単一の行のみを選択するtableView.selectRow(2)ため、この状況では機能しません。selectRow()

利用できる方法がありselectionModel().select()ます。QSelectionItemただし、 s オブジェクトを引数として受け入れます。QSelectionItem行番号を持つオブジェクトをどのように宣言しますか?

4

3 に答える 3

5

コードは と を作成しQTableViewますQPushButton。ボタンを押すと、連続した順序でインデックスが選択されます (からindex1までindex2)。任意の順序でインデックスを選択できるかどうかは、まだ答えのない問題です。

ここに画像の説明を入力

def clicked():
    tableView.setFocus()
    selectionModel = tableView.selectionModel()
    index1 = tableView.model().index(0, 0)
    index2 = tableView.model().index(1, 2)
    itemSelection = QtGui.QItemSelection(index1, index2)
    selectionModel.select(itemSelection, QtGui.QItemSelectionModel.Rows | QtGui.QItemSelectionModel.Select)

app = QtGui.QApplication([])
window = QtGui.QWidget()
window.resize(400, 300)
tableView = QtGui.QTableView()

model = QtGui.QStandardItemModel(4, 2)
for row in range(0, 4):
    for column in range(0, 3):
        item = QtGui.QStandardItem("%s , %s"%(row, column))
        model.setItem(row, column, item)

tableView.setModel(model)

selectionModel = QtGui.QItemSelectionModel(model)
tableView.setSelectionModel(selectionModel)

button = QtGui.QPushButton('Select from 0,0 to 1,2')
button.clicked.connect(clicked)
layout = QtGui.QVBoxLayout()
layout.addWidget(tableView)
layout.addWidget(button)
window.setLayout(layout)
window.show()

app.exec_()
于 2016-05-05T04:21:03.877 に答える