2012-11-06 3 views
2

내가 커스텀 모델로 QTableView을 구현 그리고 난이 보이는 셀 필요 틱 : 현재 http://oi47.tinypic.com/2jirkw.jpgPySide 텍스트 QTableView에 위임하고

나는 아래의 대리자를 사용 (다른 답변에서 적응을)에 단일 셀에 플래그 (html 텍스트)를 그려 전체 셀을 클릭하면 플래그가 변경됩니다.

class HTMLDelegate(QtGui.QStyledItemDelegate): 
    def Doc(self, options): 
     if options.text == "0": 
      return "" 
     elif options.text == "1": 
      return "<b><font align='right' color='green'>V</font></b>" 
     elif options.text == "2": 
      return "<font align='right' color='#FF8400'>!!</font>" 
     elif options.text == "3": 
      return "<b><font align='right' color='#CC0000'>F</font></b>" 
     elif options.text == "4": 
      return "<b><font align='right' color='#000000'>X</font></b>" 

    def paint(self, painter, option, index): 
     options = QtGui.QStyleOptionViewItemV4(option) 
     self.initStyleOption(options,index) 

     style = QtGui.QApplication.style() if options.widget is None else options.widget.style() 

     doc = QtGui.QTextDocument() 

     background = None 
     doc.setHtml(self.Doc(options)) 

     options.text = "" 
     style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter); 

     ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() 

     # Highlighting text if item is selected 
     #if (optionV4.state & QStyle::State_Selected) 
     # ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText)); 

     textRect = style.subElementRect(QtGui.QStyle.SE_ItemViewItemText, options, None) 
     painter.save() 
     if background: 
      painter.fillRect(options.rect, QtCore.Qt.yellow) 
     painter.translate(textRect.topLeft()) 
     painter.setClipRect(textRect.translated(-textRect.topLeft())) 

     doc.documentLayout().draw(painter, ctx) 

     painter.restore() 

    def editorEvent(self, event, model, option, index): 
     if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0: 
      return False 

     # Do not change the checkbox-state 
     if event.type() == QtCore.QEvent.MouseButtonRelease or event.type() == QtCore.QEvent.MouseButtonDblClick: 
      if event.button() != QtCore.Qt.LeftButton: 
       return False 
      if event.type() == QtCore.QEvent.MouseButtonDblClick: 
       return True 
     elif event.type() == QtCore.QEvent.KeyPress: 
      if event.key() != QtCore.Qt.Key_Space and event.key() != QtCore.Qt.Key_Select: 
       return False 
     else: 
      return False 

     # Change the flag-state 
     self.setModelData(None, model, index) 
     return False 


    def setModelData (self, editor, model, index): 
     ''' 
     The user wanted to change the old flag into the next 
     ''' 
     #newValue = not bool(index.model().data(index, QtCore.Qt.DisplayRole)) 
     #model.setData(index, newValue, QtCore.Qt.EditRole) 
     oldFlagIndex = model.index(index.row(), COD_COL_FLAGS) 
     oldFlag = model.data(oldFlagIndex) 

     if type(oldFlag) == long: 
      newFlag = oldFlag + 1 
      if newFlag > 4: 
       newFlag = 0 
     else: 
      newFlag = 0 
     model.setData(index, newFlag, QtCore.Qt.EditRole) 

어떻게하면됩니까? 내가 생각하고있는
옵션은 다음과 같습니다

  • 권리

  • 두 텍스트 문자열을 그립니다 대리자에 픽스맵과 함께 (부자 또는 단순) 텍스트를 그립니다 대리인 (하나 클릭하면 우측 연습으로

에 왼쪽에 하나는 정렬하지만, 앱 필요 없음) 정상과 한 부자, 또는 둘 모두 서식있는 텍스트, 단지 플래그 변화를 실행하는 데 좋은 것 그것에 대신 wh 올 셀.

감사

+0

다른 답변은 무엇입니까? 링크를 제공하는 것이 도움이 될 것입니다. – neuronet

답변

1

페인트 이벤트는 RECT 객체를 포함해야 인수라는 옵션이 있습니다. 그 rect 객체는 그려지는 것에 대한 경계입니다. 해당 rect의 너비를 변경하고 두 번째 파트를 그릴 부분에 대한 새로운 rect를 만들 수 있어야합니다.

rect = options.rect 
options.rect = QtCore.QRect(rect.x(), rect.y(), rect.width()/2, rect.height()) 
style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter); 
options.rect = QtCore.QRect(rect.x()+rect.width()/2, rect.y(), rect.width()/2, rect.height()) 
options.text = "right side" 
style.drawControl(QtGui.QStyle.CE_ItemViewItem, options, painter);