如何控制QTableView项目的大纲

时间:2014-12-17 02:22:33

标签: python qt pyqt pyside qtableview

下面的示例代码会创建一个与QTableView相关联的QAbstractTableModel

enter image description here

黑色背景正在应用:

p=self.palette()
p.setColor(self.backgroundRole(), QtGui.QColor("#0F0F0F"))
self.setPalette(p) 

不幸的是,每个tableview项目周围都有一个白色的轮廓/边框(" Animals"," Birds"" Fish")。

问题:如何为项目的白色边框着色?

    import sys, os
    from PyQt4 import QtCore, QtGui
    app=QtGui.QApplication(sys.argv)


    class TableModel(QtCore.QAbstractTableModel):
        def __init__(self):
            QtCore.QAbstractTableModel.__init__(self)        
            self.items=['Animals','Birds','Fish']

        def rowCount(self, parent=QtCore.QModelIndex()):   
            return len(self.items)
        def columnCount(self, index=QtCore.QModelIndex()):
            return 1

        def data(self, index, role):
            if not index.isValid() or not (0<=index.row()<len(self.items)):
                return QtCore.QVariant()

            item=str(self.items[index.row()])

            if role==QtCore.Qt.UserRole:
                return item
            if role==QtCore.Qt.DisplayRole:
                return item
            if role==QtCore.Qt.TextColorRole:
                return QtCore.QVariant(QtGui.QColor(QtCore.Qt.white))

        def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
            if role!=QtCore.Qt.DisplayRole:   return QtCore.QVariant()
            if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('My Column Name') 

    class TableView(QtGui.QTableView):
        def __init__(self, parent=None):
            super(TableView, self).__init__(parent)

            self.setBackgroundRole(QtGui.QPalette.Base)

            p=self.palette()
            p.setColor(self.backgroundRole(), QtGui.QColor("#0F0F0F"))
            self.setPalette(p)

            myModel=TableModel()
            self.setModel(myModel)       

            self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
            self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)


    view=TableView()
    view.show()   
    sys.exit(app.exec_())

以后编辑:

以下是实施CSS方法的工作代码:

enter image description here

from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)        
        self.items=['Animals','Birds','Fish']

    def rowCount(self, parent=QtCore.QModelIndex()):   
        return len(self.items)
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)):
            return QtCore.QVariant()

        item=str(self.items[index.row()])

        if role==QtCore.Qt.UserRole:
            return item
        if role==QtCore.Qt.DisplayRole:
            return item
        if role==QtCore.Qt.TextColorRole:
            return QtCore.QVariant(QtGui.QColor(QtCore.Qt.white))

    def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
        if role!=QtCore.Qt.DisplayRole:   return QtCore.QVariant()
        if orientation==QtCore.Qt.Horizontal: return QtCore.QVariant('My Column Name') 

class TableView(QtGui.QTableView):
    def __init__(self, parent=None):
        super(TableView, self).__init__(parent)
        self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
        self.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)        

        myModel=TableModel()
        self.setModel(myModel)      

        appStyle="""
        QTableView
        {   
            background-color: black;
            gridline-color:black;
            color: black;
            selection-color: black;
        }
        QTableView::item 
        {   
            color: white;
            background:black;            
        }
        QTableView::item:hover
        {   
            color: black;
            background:#ffaa00;            
        }
        QTableView::item:focus
        {   
            color: black;
            background:#0063cd;            
        }        
        """
        self.setStyleSheet(appStyle)

view=TableView()
view.show()   
sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:3)

Qt样式表非常强大的样式,我推荐它们。与

self.setStyleSheet('QTableView::item{background:black;} QTableView{gridline-color:black;}')
在您的示例中,

而不是setPalette可以实现表格的黑色项目和黑色网格线。您可以使用样式表执行更多操作,例如在选中时更改项目的背景。请参阅stylesheet reference