如何将QTableView项的方向从垂直更改为水平?

2024-09-25 14:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码创建了一个窗口,其中并排放置了两个QTableViews:

enter image description here

左侧的TableView链接到QAbstractTableModel。根据Model.data()中定义的逻辑,tableView的项被垂直定位。在

{cd3>链接到视图。我想用它来改变垂直的TableView项目的位置为水平。在

请发表你的建议,以实现这一目标。如果改变项目方向的解决方案不需要代理模型。。。只要有效!在

稍后发布的源代码

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_())

Tags: selfdataindexmodelreturninitdefargs
2条回答

一个有趣的任务。。。在

class Model2(QAbstractTableModel):
    def __init__(self, model, parent=None):
        self.model = model
        QAbstractTableModel.__init__(self, parent)

    def rowCount(self):
        return self.model.columnCount()

    def columnCount(self):
        return self.model.rowCount()

    def data(self, a, b):
        return self.model.data(b, a)

以下是使用代理将项目从垂直方向重新定向到水平的工作解决方案。在

诀窍是重写soruce模型的.data()方法,方法是在代理模型下声明它。在代理上声明时,它的优先级高于源模型的.data()方法:

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 flags(self, index):
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

    def rowCount(self, parent):
        return len(self.items)       
    def columnCount(self, parent):
        return len(self.items)  

    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()

    def setData(self, index, value, role=Qt.EditRole):
        if index.isValid():            
            if role == Qt.EditRole:                
                row = index.row()
                self.items[row]=value  
                return True
        return False

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

    def data(self, index, role):
        sourceModel=self.sourceModel()
        items=sourceModel.items

        if not index.isValid(): return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        column=index.column()

        if column<len(items):
            return QVariant(items[column])
        else:
            return QVariant()


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_())

这是第二个受mdurant答案启发的解决方案。我在这里起诉两个。第二个模型查询第一个模型self.items数据变量。在

^{pr2}$

相关问题 更多 >