0
的代碼創建一個單一的QTableView
分配給QAbstractTableModel
:如何使用模型控制表格視圖中的標題背景顏色?
問題: 1.如何改變標題背景色爲藍色的? 2.如何將TableView的下半部分(現在是白色)更改爲橙色。
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=['One','Two','Three','Four','Five','Six','Seven']
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))
if role == QtCore.Qt.BackgroundRole:
if index.row()%2:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.gray))
else:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.darkGray))
if role == QtCore.Qt.TextAlignmentRole:
return (QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
def headerData(self, column, orientation, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.TextAlignmentRole:
return (QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
if role == QtCore.Qt.BackgroundRole:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.blue))
if role == QtCore.Qt.ForegroundRole:
if orientation == QtCore.Qt.Horizontal:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.red))
elif orientation == QtCore.Qt.Vertical:
return QtCore.QVariant(QtGui.QColor(QtCore.Qt.green))
if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Horizontal:
return QtCore.QString('Horizont Column')
if role == QtCore.Qt.DisplayRole and orientation == QtCore.Qt.Vertical:
return QtCore.QString('Vertical Column')
if role == QtCore.Qt.FontRole:
return QtGui.QFont('Times', pointSize=5, weight=-1, italic=True)
class TableView(QtGui.QTableView):
def __init__(self, parent=None):
super(TableView, self).__init__(parent)
self.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
myModel=TableModel()
self.setModel(myModel)
view=TableView()
view.show()
sys.exit(app.exec_())
了不起的顏色分開!謝謝!我仍然想知道如何實現相同的使用模型。 – alphanumeric