The PyQt4 code porting of Model/View Programming e

import sys from PyQt4.QtGui import * from PyQt4.QtCore import * if __name__ == '__main__': app =QApplication(sys.argv) splitter =QSplitter() model= QFileSystemModel () model.setRootPath(QDir.currentPath()) tree = QTreeView(splitter); tree.setModel(model); tree.setRootIndex(model.index(QDir.currentPath())); list = QListView(splitter); list.setModel(model); list.setRootIndex(model.index(QDir.currentPath())); splitter.setWindowTitle("Two views onto the same file system model"); splitter.show() app.exec_()

 

selectionChanged() and currentChanged()

import sys from PyQt4.QtGui import * from PyQt4.QtCore import * COLS= 4 ROWS= 8 class MyModel(QAbstractTableModel): def __init__(self, parent=None): super(MyModel, self).__init__(parent) self.m_gridData=[["Row{}, Column{}".format(row, column) for column in range(COLS)] for row in range(ROWS)] def rowCount(self, parent = QModelIndex()): return ROWS def columnCount(self,parent = QModelIndex()) : return COLS def data(self,index, role = Qt.DisplayRole) : if (role == Qt.DisplayRole): return self.m_gridData[index.row()][index.column()] return None def setData(self, index,value,role= Qt.EditRole): if (role == Qt.EditRole): self.m_gridData[index.row()][index.column()] = str(value) return True def flags(self, index): return Qt.ItemIsSelectable Qt.ItemIsEditable Qt.ItemIsEnabled ; if __name__ == '__main__': app =QApplication(sys.argv) tableView=QTableView() myModel = MyModel (None); tableView.setModel( myModel ) def updateSelection(selected,deselected): items = selected.indexes() for index in items : print(index.row(), index.column()) text = "({},{})".format(index.row(), index.column()) myModel .setData(index, text) items = deselected.indexes() for index in items : myModel .setData(index, '---') selectionModel = tableView.selectionModel() topLeft = myModel .index(0, 0, QModelIndex()); bottomleft= myModel.index(7, 0, QModelIndex()); selection1=QItemSelection (topLeft,bottomleft); selectionModel.select(selection1, QItemSelectionModel.Select); topLeft1= myModel .index(0, 1, QModelIndex()); bottomRight = myModel.index(5, 2, QModelIndex()); selection=QItemSelection (topLeft1, bottomRight); selectionModel.selectionChanged.connect(updateSelection) selectionModel.select(selection1, QItemSelectionModel.Deselect); selectionModel.select(selection, QItemSelectionModel.Select); def changeCurrent(current,previous): tableView.setWindowTitle("Moved from ({},{}) to ({},{})".format(previous.row(), previous.column(), current.row(), current.column())) selectionModel.currentChanged.connect(changeCurrent) tableView.show(); sys.exit(app.exec_())

using the Toggle command to invert the selection state of the items given.

import sys from PyQt4.QtGui import * from PyQt4.QtCore import * COLS= 4 ROWS= 8 class MyModel(QAbstractTableModel): def __init__(self, parent=None): super(MyModel, self).__init__(parent) self.m_gridData=[["Row{}, Column{}".format(row, column) for column in range(COLS)] for row in range(ROWS)] def rowCount(self, parent = QModelIndex()): return ROWS def columnCount(self,parent = QModelIndex()) : return COLS def data(self,index, role = Qt.DisplayRole) : if (role == Qt.DisplayRole): return self.m_gridData[index.row()][index.column()] return None def setData(self, index,value,role= Qt.EditRole): if (role == Qt.EditRole): self.m_gridData[index.row()][index.column()] = str(value) return True def flags(self, index): return Qt.ItemIsSelectable Qt.ItemIsEditable Qt.ItemIsEnabled ; if __name__ == '__main__': app =QApplication(sys.argv) tableView=QTableView() myModel = MyModel (None); tableView.setModel( myModel ) selectionModel = tableView.selectionModel() topLeft = myModel .index(0, 0, QModelIndex()); bottomleft= myModel.index(7, 0, QModelIndex()); selection1=QItemSelection (topLeft,bottomleft); selectionModel.select(selection1, QItemSelectionModel.Select); topLeft1= myModel .index(0, 0, QModelIndex()); bottomRight = myModel.index(5, 2, QModelIndex()); selection=QItemSelection (topLeft1, bottomRight); selectionModel.select(selection, QItemSelectionModel.Toggle); tableView.show(); sys.exit(app.exec_())

the use of the Rows and Columns flags

import sys from PyQt4.QtGui import * from PyQt4.QtCore import * COLS= 4 ROWS= 8 class MyModel(QAbstractTableModel): def __init__(self, parent=None): super(MyModel, self).__init__(parent) self.m_gridData=[["Row{}, Column{}".format(row, column) for column in range(COLS)] for row in range(ROWS)] def rowCount(self, parent = QModelIndex()): return ROWS def columnCount(self,parent = QModelIndex()) : return COLS def data(self,index, role = Qt.DisplayRole) : if (role == Qt.DisplayRole): return self.m_gridData[index.row()][index.column()] return None def setData(self, index,value,role= Qt.EditRole): if (role == Qt.EditRole): self.m_gridData[index.row()][index.column()] = str(value) return True def flags(self, index): return Qt.ItemIsSelectable Qt.ItemIsEditable Qt.ItemIsEnabled if __name__ == '__main__': app =QApplication(sys.argv) tableView=QTableView() myModel = MyModel (None) tableView.setModel( myModel ) selectionModel = tableView.selectionModel() columnSelection=QItemSelection() topLeft = myModel.index(0, 1, QModelIndex()) bottomRight = myModel.index(0, 2, QModelIndex()) columnSelection.select(topLeft, bottomRight) selectionModel.select(columnSelection, QItemSelectionModel.Select QItemSelectionModel.Columns) rowSelection=QItemSelection () topLeft = myModel.index(0, 0, QModelIndex()) bottomRight = myModel.index(1, 0, QModelIndex()) rowSelection.select(topLeft, bottomRight) selectionModel.select(rowSelection, QItemSelectionModel.Select QItemSelectionModel.Rows) tableView.show() sys.exit(app.exec_())

 

Creating new models
评论

© ID4333709 | Powered by LOFTER