Is it possible to set the text of the QTableView corner button?

前端 未结 2 444
野的像风
野的像风 2021-01-06 08:00

QTableView has a corner button, occupying the intersection between the horizontal and the vertical header. Clicking this will select all cells in the table. What I\'m wonder

相关标签:
2条回答
  • 2021-01-06 08:36

    You can add text on the corner button by finding a child from QTableWidget and add a QLabel to it by using the vertical layout as below.

    QAbstractButton* button = qFindChild< QAbstractButton* >(TableWidget);
    if (button) 
    {
        QVBoxLayout* lay = new QVBoxLayout(button);
        lay->setContentsMargins(0, 0, 0, 0);
        QLabel* label = new QLabel("No");
        label->setStyleSheet("QLabel {font-face: ArialMT; font-size: 10px; color: #FFFFFF; font-weight: bold; }""QToolTip { color: #ffffff; background-color: #000000; border: 1px #000000; }");
        label->setAlignment(Qt::AlignCenter);
        label->setToolTip("Text");
        label->setContentsMargins(2, 2, 2, 2);
        lay->addWidget(label);
    }
    
    0 讨论(0)
  • 2021-01-06 08:55

    I have implemented a working solution with PyQt 5.3, and it took surprisingly little code. My solution is based on code posted in this question at Qt Centre.

    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 讨论(0)
提交回复
热议问题