2012-10-29 3 views
1

단순한 대리인이 작동하는 것처럼 보이는 것을 얻으려고 애 쓰고 있습니다.TableView Cell Delegates (Qt.BackgroundRole)

내가 원하는 것은 tableview 셀의 배경을 변경하는 것입니다. 내가 이해 한 것에서 Qt.BackgroundRole을 조사해야합니다. 지금까지 나는 그것을 작동 시키거나 관련 예제를 찾을 수 없었다.

내가 지금까지 가지고있는 것은 셀에 색을 채우는 대리자이지만 텍스트 위에 이있는 것 같습니다. 내가 원했던 것은 텍스트를 그대로두고 셀의 배경 만 바꾸는 것입니다.

class CellBackgroundColor(QtGui.QStyledItemDelegate): 

    def __init__(self, parent = None): 
     QtGui.QStyledItemDelegate.__init__(self, parent) 

    def paint(self, painter, option, index):  

     path = index.model().data(index, QtCore.Qt.DisplayRole).toString() 
     painter.fillRect(option.rect, QtGui.QColor(path)) 

이 Qt.BackgroundRole을 tableview 대리자에 구현하는 방법에 대한 아이디어가 있으십니까? 사전에

감사합니다, 크리스

답변

2

당신은 셀 배경을 페인트 대리자를 사용할 필요가 없습니다; 기본 위임은 Qt.BackgroundRole을 통해 배경 그림을 지원

class MyTableModel(QtCore.QAbstractTableModel): 
    ... 
    def data(self, index, role=QtCore.Qt.DisplayRole): 
     if role == QtCore.Qt.BackgroundRole: 
      return QtGui.QColor(...) 

그렇지 않으면이 QStyleOptionViewItem을 초기화하고 적절한 재정으로 그 페인트 initStyleOption를 사용하는 것이 좋습니다 :

class CellBackgroundColor(QtGui.QStyledItemDelegate): 
    ... 
    def paint(self, painter, option, index): 
     self.initStyleOption(option, index) 
     # override background 
     option.backgroundBrush = QtGui.QColor(...) 
     widget = option.widget 
     style = widget.style() 
     style.drawControl(QtGui.QStyle.CE_ItemViewItem, option, painter, widget) 
+0

안녕 ecatmur, 감사를위한 많은 당신의 댓글. 어떻게 두 번째 예제에서 배경을 재정의합니까? – user1028826

+0

@ user1028826 당신은'backgroundBrush'를 설정할 수 있습니다 : http://doc.qt.digia.com/qt/qstyleoptionviewitemv4.html#backgroundBrush-var – ecatmur

+0

고마워요, 지금 두번째 예제는 완벽하게 작동합니다. 건배는 멋진 한 주를 보낸다! – user1028826