2014-12-19 6 views
0

아래 예제 코드는 단일 QTableView를 만듭니다.QSS를 사용하여 테이블보기의 배경색을 교체하는 방법은 무엇입니까?

enter image description here

질문 : 홀수 항목의 배경 색상 회색과 짝수 항목을 검정하기 위해이 코드를 수정하는 방법. CSS의 alternate-background-color을 사용해야합니까? 저기있는 깃발?

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

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

특히 PyQt에 대해서는 언급 할 수 없지만 모델에 배경을 저장하는 한 가지주의 사항은 모든보기로 넘어가는 것입니다. MVC의 본질 인 여러 가지 방식으로 동일한 데이터를 표시하려면 delegate에서 수행해야합니다 (paint()를 다시 구현). –

답변

1

다음은 모델을 사용하여 항목의 배경색을 제어하는 ​​방법입니다. CSS는 나중에 다른 모든 것에 사용됩니다.

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("#242424")) 
      else: 
       return QtCore.QVariant(QtGui.QColor(QtCore.Qt.black)) 

    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:grey; 
      color: black; 
     } 
     QTableView::item 
     { 
      color: white;   
     } 
     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_()) 
관련 문제