2014-03-25 1 views
4

QTableView에는 가로 및 세로 머리글 간의 교차 부분을 차지하는 corner button이 있습니다. 이것을 클릭하면 테이블의 모든 셀이 선택됩니다. 내가 궁금해하는 점은이 버튼의 텍스트를 설정할 수 있는지, 그렇다면 어떻게 할 것인가?QTableView 모서리 단추의 텍스트를 설정할 수 있습니까?

+1

참조 [QT 중심이 질문] (http://www.qtcentre.org/threads/6252-QTableWidget-NW-corner-header-item). – thuga

답변

3

필자는 PyQt 5.3을 사용한 작업 솔루션을 구현했으며 놀라 울 정도로 적은 코드를 사용했습니다. 내 솔루션은 Qt Center this question에 게시 된 코드를 기반으로합니다.

from PyQt5 import QtWidgets, QtCore 


class TableView(QtWidgets.QTableView): 
    """QTableView specialization that can e.g. paint the top left corner header. 
    """ 
    def __init__(self, nw_heading, parent): 
     super(TableView, self).__init__(parent) 

     self.__nw_heading = nw_heading 
     btn = self.findChild(QtWidgets.QAbstractButton) 
     btn.setText(self.__nw_heading) 
     btn.setToolTip('Toggle selecting all table cells') 
     btn.installEventFilter(self) 

     opt = QtWidgets.QStyleOptionHeader() 
     opt.text = btn.text() 
     s = QtCore.QSize(btn.style().sizeFromContents(
      QtWidgets.QStyle.CT_HeaderSection, opt, QtCore.QSize(), btn). 
      expandedTo(QtWidgets.QApplication.globalStrut())) 

     if s.isValid(): 
      self.verticalHeader().setMinimumWidth(s.width()) 

    def eventFilter(self, obj, event): 
     if event.type() != QtCore.QEvent.Paint or not isinstance(
       obj, QtWidgets.QAbstractButton): 
      return False 

     # Paint by hand (borrowed from QTableCornerButton) 
     opt = QtWidgets.QStyleOptionHeader() 
     opt.initFrom(obj) 
     styleState = QtWidgets.QStyle.State_None 
     if obj.isEnabled(): 
      styleState |= QtWidgets.QStyle.State_Enabled 
     if obj.isActiveWindow(): 
      styleState |= QtWidgets.QStyle.State_Active 
     if obj.isDown(): 
      styleState |= QtWidgets.QStyle.State_Sunken 
     opt.state = styleState 
     opt.rect = obj.rect() 
     # This line is the only difference to QTableCornerButton 
     opt.text = obj.text() 
     opt.position = QtWidgets.QStyleOptionHeader.OnlyOneSection 
     painter = QtWidgets.QStylePainter(obj) 
     painter.drawControl(QtWidgets.QStyle.CE_Header, opt) 

     return True 
+0

QTableView의 cornerButton에 대한 QtWidgets.QAbstractButton의 모든 메소드와 함수에 대한 액세스를 엽니 다. –

관련 문제