-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectItem.py
66 lines (43 loc) · 1.93 KB
/
SelectItem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sys
from PyQt4.QtCore import QModelIndex
from PyQt4.QtGui import *
class Window(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
self.model = QStandardItemModel(8, 4)
table = QTableView()
table.setModel(self.model)
actionMenu = QMenu(self.tr("&Actions"), self)
fillAction = actionMenu.addAction(self.tr("&Fill Selection"))
clearAction = actionMenu.addAction(self.tr("&Clear Selection"))
selectAllAction = actionMenu.addAction(self.tr("&Select All"))
self.menuBar().addMenu(actionMenu)
fillAction.triggered.connect(self.fillSelection)
clearAction.triggered.connect(self.clearSelection)
selectAllAction.triggered.connect(self.selectAll)
self.selectionModel = table.selectionModel()
self.statusBar()
self.setCentralWidget(table)
self.setWindowTitle(self.tr("Selected Items in a Table Model"))
def fillSelection(self):
indexes = self.selectionModel.selectedIndexes()
for index in indexes:
text = u"(%i,%i)" % (index.row(), index.column())
self.model.setData(index, text)
def clearSelection(self):
indexes = self.selectionModel.selectedIndexes()
for index in indexes:
self.model.setData(index, "")
def selectAll(self):
parent = QModelIndex()
topLeft = self.model.index(0, 0, parent)
bottomRight = self.model.index(
self.model.rowCount(parent) - 1,
self.model.columnCount(parent) - 1, parent)
selection = QItemSelection(topLeft, bottomRight)
self.selectionModel.select(selection, QItemSelectionModel.Select)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())