Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以设置QTableView角按钮的文本?_Qt_Pyqt_Qt5_Pyqt5 - Fatal编程技术网

是否可以设置QTableView角按钮的文本?

是否可以设置QTableView角按钮的文本?,qt,pyqt,qt5,pyqt5,Qt,Pyqt,Qt5,Pyqt5,QTableView有一个标题,占据水平标题和垂直标题之间的交点。单击此按钮将选择表中的所有单元格。我想知道的是,是否有可能设置此按钮的文本,如果有,如何设置?我已经用PyQt 5.3实现了一个可行的解决方案,而且所需的代码非常少。我的解决方案基于Qt中心发布的代码 from PyQt5 import QtWidgets, QtCore class TableView(QtWidgets.QTableView): """QTableView specialization that c

QTableView有一个标题,占据水平标题和垂直标题之间的交点。单击此按钮将选择表中的所有单元格。我想知道的是,是否有可能设置此按钮的文本,如果有,如何设置?

我已经用PyQt 5.3实现了一个可行的解决方案,而且所需的代码非常少。我的解决方案基于Qt中心发布的代码

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

您可以通过从QTableWidget中查找一个子项在角落按钮上添加文本,并通过使用垂直布局向其添加QLabel,如下所示

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

请参阅。它打开了对QtWidgets的所有方法和功能的访问。QatableView的cornerButton的QAbstractButton。