【问题标题】:How to control QTableView Items Background color如何控制 QTableView 项目的背景颜色
【发布时间】:2015-03-16 09:09:16
【问题描述】:

如果索引的行号为偶数,则源模型的data() 将每个`QTableView 的索引背景颜色设置为绿色,如果索引的行号为奇数,则设置为蓝色。

然后代理模型过滤掉每三个索引。所以结果颜色都是无序的。

问题是在代理模型过滤索引之前,在源模型中分配了背景颜色。

这里是源代码:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyTableModel(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = [i for i in range(90)]

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

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

        if role == Qt.DisplayRole:
            if row<len(self.items):
                return QVariant(self.items[row])
            else:
                return QVariant()

        if role==Qt.BackgroundColorRole:
            if row%2: bgColor=QColor(Qt.green)
            else: bgColor=QColor(Qt.blue)        
            return QVariant(QColor(bgColor))


class Proxy01(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy01, self).__init__()

    def filterAcceptsRow(self, row, parent):
        if row%3: return True
        else: return False

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        self.tablemodel=MyTableModel(self)               

        self.proxy1=Proxy01()
        self.proxy1.setSourceModel(self.tablemodel)

        tableviewA=QTableView(self) 
        tableviewA.setModel(self.proxy1)
        tableviewA.setSortingEnabled(True) 
        tableviewA.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)
        tableviewA.horizontalHeader().setStretchLastSection(True)

        layout = QVBoxLayout(self)
        layout.addWidget(tableviewA)

        self.setLayout(layout)

    def test(self, arg):
        print arg

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_()) 

【问题讨论】:

    标签: python qt model pyqt qtableview


    【解决方案1】:

    而不是依赖源模型的data()if Qt.BackgroundColorRole: 方法的功能将tableviewA.setAlternatingRowColors(True) 设置为True。它与 CSS 完美结合。下面发布了一个完全可行的解决方案(请注意Qt.BackgroundColorRole 已被注释掉。否则它将优先于 CSS):

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import sys
    
    class MyTableModel(QAbstractTableModel):
        def __init__(self, parent=None, *args):
            QAbstractTableModel.__init__(self, parent, *args)
            self.items = [i for i in range(90)]
    
        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()
    
            row=index.row()
            column=index.column()
    
            if role == Qt.DisplayRole:
                if row<len(self.items):
                    return QVariant(self.items[row])
                else:
                    return QVariant()
    
            # if role==Qt.BackgroundColorRole:
            #     if row%2: bgColor=QColor(Qt.green)
            #     else: bgColor=QColor(Qt.blue)        
            #     return QVariant(QColor(bgColor))
    
    
    class Proxy01(QSortFilterProxyModel):
        def __init__(self):
            super(Proxy01, self).__init__()
    
        def filterAcceptsRow(self, row, parent):
            if row%3: return True
            else: return False
    
    class MyWindow(QWidget):
        def __init__(self, *args):
            QWidget.__init__(self, *args)
    
            self.tablemodel=MyTableModel(self)               
    
            self.proxy1=Proxy01()
            self.proxy1.setSourceModel(self.tablemodel)
    
            tableviewA=QTableView(self) 
            tableviewA.setModel(self.proxy1)
            tableviewA.setSortingEnabled(True) 
            tableviewA.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)
            tableviewA.horizontalHeader().setStretchLastSection(True)
            tableviewA.setAlternatingRowColors(True)
            tableviewA.setStyleSheet("alternate-background-color: yellow; background-color: red;");
    
            layout = QVBoxLayout(self)
            layout.addWidget(tableviewA)
    
            self.setLayout(layout)
    
        def test(self, arg):
            print arg
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = MyWindow()
        w.show()
        sys.exit(app.exec_()) 
    

    【讨论】:

    • 例如是否可以为高于 10 的行设置一种颜色,然后其余部分保持不变(即前 10 行的背景为白色)?谢谢你告诉我。
    • NVM,想通了,在我的情况下,数据方法的实现必须如下:如果角色 == Qt.BackgroundColorRole 并且 self.r 不是无:bgColor=QColor(Qt.blue) 如果index.row() > self.r: return QVariant(QColor(bgColor))
    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    相关资源
    最近更新 更多