The code below creates a window with two QTableViews placed side-by-side:
The left-side TableView is linked to QAbstractTableModel
. And according to a logic defined in Model.data()
the tableView's items are positioned vertically.
The right-side TableView is linked to a QSortFilterProxyModel
. And I want to use it to change the vertical TableView items placement to horizontal.
Please post your suggestions on how that could be achieved. It's OK if the solution on changing the item's orientation wouldn't require ProxyModel... as long as it works!
SOURCE CODE POSTED LATER.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class Model(QAbstractTableModel):
def __init__(self, parent=None, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.items = ['Row0_Column0','Row0_Column1','Row0_Column2']
def rowCount(self, parent):
return len(self.items)
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid(): return QVariant()
elif role != Qt.DisplayRole:
return QVariant()
row=index.row()
if row<len(self.items):
return QVariant(self.items[row])
else:
return QVariant()
class Proxy(QSortFilterProxyModel):
def __init__(self):
super(Proxy, self).__init__()
def rowCount(self, parent):
return 1
def columnCount(self, parent):
sourceModel=self.sourceModel()
return len(sourceModel.items)
def filterAcceptsRow(self, row, parent):
sourceModel=self.sourceModel()
sourceModelIndex=sourceModel.index(row, 0, QModelIndex())
sourceModelIndexName=sourceModel.data(sourceModelIndex, Qt.DisplayRole).toString()
return True
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
tablemodel=Model(self)
proxy=Proxy()
proxy.setSourceModel(tablemodel)
tableviewA=QTableView()
tableviewA.setModel(tablemodel)
tableviewB=QTableView()
tableviewB.setModel(proxy)
layout = QHBoxLayout(self)
layout.addWidget(tableviewA)
layout.addWidget(tableviewB)
self.setLayout(layout)
def test(self, arg):
print arg
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())