from PySide2 import QtWidgets, QtGui, QtCore
from PySide2.QtCore import Qt
import math
import sys
def __init__(self):
super(CustomTableView, self).__init__()
self.setMouseTracking(True)
def mousePressEvent
(self, event
: QtGui.
QMouseEvent) -> None
: if event.button() == Qt.LeftButton:
super(CustomTableView, self).mousePressEvent(event)
elif event.button() == Qt.RightButton:
self.doSomethingWithSelected()
def doSomethingWithSelected(self):
for ix in self.selectionModel().selectedIndexes():
print("Selected Indexes:", ix.row(), ix.column(), ix.data(Qt.DisplayRole))
def __init__(self, parent=None):
super(CustomListModel, self).__init__(parent)
self._data = [
"Asset_A1",
"Asset_A2",
"Asset_A3",
"Asset_B1",
"Asset_B2",
"Asset_B3",
"Asset_C1",
"Asset_C2",
"Asset_C3",
"Asset_C4",
"Asset_D1",
"Asset_D2",
"Asset_D3",
"Asset_D4",
]
return len(self._data)
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self._data[index.row()]
class CustomList2TableProxyModel(QtCore.QIdentityProxyModel):
def __init__(self, columns=1, parent=None):
super(CustomList2TableProxyModel, self).__init__(parent)
self._columns = columns
return self._columns
if parent.isValid():
return 0
return math.ceil(self.sourceModel().rowCount()/self.columnCount())
def mapToSource(self, proxyIndex):
if proxyIndex.isValid():
r = proxyIndex.row()
c = proxyIndex.column()
row = r * self.columnCount() + c
return self.sourceModel().index(row, 0)
def mapFromSource(self, sourceIndex):
r = math.ceil(sourceIndex.row()/self.columnCount())
c = sourceIndex.row() % self.columnCount()
return self.index(r, c)
def data(self, index, role=Qt.DisplayRole):
r = index.row()
c = index.column()
row = r*self.columnCount() + c
if row < self.sourceModel().rowCount():
return super(CustomList2TableProxyModel, self).data(index, role)
def index
(self, row, column, parent
=QtCore.
QModelIndex()): if parent.isValid():
return self.createIndex(row, column)
# def sibling(self, row: int, column: int, idx: QtCore.QModelIndex) -> QtCore.QModelIndex:
# print("Sibling", row, column, idx.data(Qt.DisplayRole))
def __init__(self):
super(CustomMainWindow, self).__init__()
self.initUI()
def initUI(self):
self.
mainWidget = QtWidgets.
QWidget()
# List Model
self.
listViewWidget = QtWidgets.
QWidget() sourceModel = CustomListModel()
self.listView.setModel(sourceModel)
self.
listViewLayout.
addWidget(QtWidgets.
QLabel("QAbstractListModel: Source model")) self.listViewLayout.addWidget(self.listView)
self.listViewWidget.setLayout(self.listViewLayout)
# Filter model
self.
filterListViewWidget = QtWidgets.
QWidget() self.filterProxy.setSourceModel(sourceModel)
self.
filterProxy.
setFilterRegExp(QtCore.
QRegExp("1", Qt.
CaseInsensitive, QtCore.
QRegExp.
FixedString)) self.filterProxy.setFilterKeyColumn(0)
self.filterListView.setModel(self.filterProxy)
self.
filterListViewLayout.
addWidget(QtWidgets.
QLabel("QSortFilterProxyModel: Filter '1'")) self.filterListViewLayout.addWidget(self.filterListView)
self.filterListViewWidget.setLayout(self.filterListViewLayout)
# List to Grid model
self.
gridViewWidget = QtWidgets.
QWidget() self.gridView = CustomTableView()
self.gridProxy = CustomList2TableProxyModel(2)
self.gridProxy.setSourceModel(self.filterProxy)
self.gridView.setModel(self.gridProxy)
self.
gridViewLayout.
addWidget(QtWidgets.
QLabel("QIdentityProxyModel: List to table model")) self.gridViewLayout.addWidget(self.gridView)
self.gridViewWidget.setLayout(self.gridViewLayout)
# Assemble main Layout
self.mainLayout.addWidget(self.listViewWidget)
self.mainLayout.addWidget(self.filterListViewWidget)
self.mainLayout.addWidget(self.gridViewWidget)
self.mainWidget.setLayout(self.mainLayout)
self.setCentralWidget(self.mainWidget)
if __name__ == '__main__':
w = CustomMainWindow()
w.show()
sys.exit(app.exec_())