设置QT_AUTO_SCREEN_SCALE_FACTOR导致QTableWidget网格线和headerView边框线未对齐

问题描述 投票:0回答:1

示例:

文件:table.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'table.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(410, 311)
        self.gridLayout = QtWidgets.QGridLayout(Dialog)
        self.gridLayout.setObjectName("gridLayout")
        self.tableWidget = QtWidgets.QTableWidget(Dialog)
        font = QtGui.QFont()
        font.setPointSize(26)
        self.tableWidget.setFont(font)
        self.tableWidget.setFocusPolicy(QtCore.Qt.NoFocus)
        self.tableWidget.setStyleSheet("QScrollBar{\n"
"    border:1px solid #ABABAB;\n"
"}\n"
"\n"
"QScrollBar::add-page, QScrollBar::sub-page{\n"
"    background: rgb(223, 223, 223);\n"
"}\n"
"\n"
"QScrollBar:vertical {\n"
"   width: 13px;\n"
"   margin: 14px 0 14px 0; \n"
"}\n"
"\n"
"QScrollBar::handle:vertical {\n"
"   background:rgb(250,250,250);\n"
"   border:1px solid #ABABAB;\n"
"   border-left:none;\n"
"   border-right:none;\n"
"}\n"
"   \n"
"QScrollBar::add-line:vertical {\n"
"   background:rgb(249,249,249);\n"
"   height: 13px;\n"
"   subcontrol-position: bottom;\n"
"   subcontrol-origin: margin; \n"
"   border:1px solid #ABABAB;\n"
"}\n"
"\n"
"QScrollBar::sub-line:vertical {\n"
"   background: rgb(249,249,249);\n"
"   height: 13px;\n"
"   subcontrol-position: top;\n"
"   subcontrol-origin: margin;\n"
"   border:1px solid #ABABAB;\n"
"}\n"
"\n"
"QScrollBar:horizontal {\n"
"   background:rgb(223, 223, 223);\n"
"   height: 13px;\n"
"   margin: 0 14px 0 14px; \n"
"}\n"
"\n"
"QScrollBar::handle:horizontal {\n"
"   background:rgb(250,250,250);\n"
"   border:1px solid #ABABAB;\n"
"   border-top:none;\n"
"   border-bottom:none;\n"
"}\n"
"   \n"
"QScrollBar::add-line:horizontal {\n"
"   background:rgb(249,249,249);\n"
"   width: 13px;\n"
"   subcontrol-position: right;\n"
"   subcontrol-origin: margin; \n"
"   border:1px solid #ABABAB;\n"
"}\n"
"\n"
"QScrollBar::sub-line:horizontal {\n"
"   background: rgb(249,249,249);\n"
"   width: 13px;\n"
"   subcontrol-position: left;\n"
"   subcontrol-origin: margin;\n"
"   border:1px solid #ABABAB;\n"
"}\n"
"   \n"
"\n"
"QScrollBar::right-arrow,\n"
"QScrollBar::left-arrow,\n"
"QScrollBar::up-arrow,\n"
"QScrollBar::down-arrow {\n"
"    width: 6px;\n"
"    height: 6px;\n"
"    background: white;\n"
"}\n"
"\n"
"QScrollBar::right-arrow {\n"
"    image: url(:/scrollbar/assets/icons/scrollbar/arrow-right.png);\n"
"}\n"
"QScrollBar::left-arrow {\n"
"    image: url(:/scrollbar/assets/icons/scrollbar/arrow-left.png);\n"
"}\n"
"QScrollBar::up-arrow {\n"
"    image: url(:/scrollbar/assets/icons/scrollbar/arrow-up.png)\n"
"}\n"
"QScrollBar::down-arrow {\n"
"    image: url(:/scrollbar/assets/icons/scrollbar/arrow-down.png);\n"
"}\n"
"\n"
"QTableWidget{\n"
"    border:none;\n"
"}\n"
"\n"
"QHeaderView:section{\n"
"    background:rgb(220, 245, 255);\n"
"    padding:10px;\n"
"}\n"
"\n"
"QHeaderView:section::vertical{\n"
"    border:1px solid #ABABAB;\n"
"    border-top:none;\n"
"}\n"
"\n"
"QHeaderView:section:last::vertical{\n"
"    border-bottom:none;\n"
"}\n"
"\n"
"\n"
"QHeaderView:section::horizontal{\n"
"    border:none;\n"
"    border-right:1px solid #ABABAB;\n"
"}\n"
"\n"
"QHeaderView:section::horizontal:last{\n"
"    border:none;\n"
"}\n"
"\n"
"QTableWidget::item {\n"
"    padding: 10px 10px 10px 10px;\n"
"    border:none;\n"
"}\n"
"\n"
"QTableWidget QTableCornerButton::section {\n"
"    border:1px solid #ABABAB;\n"
"    border-top:none;\n"
"    border-left:none;\n"
"}")
        self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.tableWidget.setShowGrid(False)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(20)
        self.tableWidget.setRowCount(18)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(6, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(7, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(8, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(9, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(10, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(11, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(12, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(13, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(14, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(15, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(16, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setVerticalHeaderItem(17, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(5, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(6, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(7, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(8, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(9, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(10, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(11, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(12, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(13, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(14, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(15, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(16, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(17, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(18, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(19, item)
        self.tableWidget.horizontalHeader().setHighlightSections(False)
        self.gridLayout.addWidget(self.tableWidget, 0, 0, 1, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        item = self.tableWidget.verticalHeaderItem(0)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(1)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(2)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(3)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(4)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(5)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(6)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(7)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(8)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(9)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(10)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(11)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(12)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(13)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(14)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(15)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(16)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.verticalHeaderItem(17)
        item.setText(_translate("Dialog", "New Row"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(5)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(6)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(7)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(8)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(9)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(10)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(11)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(12)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(13)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(14)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(15)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(16)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(17)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(18)
        item.setText(_translate("Dialog", "New Column"))
        item = self.tableWidget.horizontalHeaderItem(19)
        item.setText(_translate("Dialog", "New Column"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

文件:run_me.py

from PyQt5 import QtWidgets, QtCore, QtGui
from table import Ui_Dialog
import os
import sys

class Run_me:
    
    def __init__(self):
        os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
        self.app = QtWidgets.QApplication(sys.argv)
        self.app.setStyle("Fusion")
        self.Dialog = QtWidgets.QDialog()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self.Dialog)
        self.Dialog.show()
        self.ui.tableWidget.setItemDelegate(GridDelegate(self.ui.tableWidget))
        sys.exit(self.app.exec_())
        
class GridDelegate(QtWidgets.QStyledItemDelegate):
    pen = QtGui.QPen(QtGui.QColor('#000000'), 1)

    def paint(self, qp, opt, index):
        qp.save()
        custom_option = QtWidgets.QStyleOptionViewItem(opt)
        custom_option.state &= ~QtWidgets.QStyle.State_Selected
        qp.setPen(self.pen)

        lastRow = index.model().rowCount() - 1
        lastCol = index.model().columnCount() - 1


        if not opt.state & QtWidgets.QStyle.State_Selected:
            qp.setBrush(QtCore.Qt.NoBrush)
        else:
            brush = QtGui.QBrush(QtGui.QColor("#1182dc"),QtCore.Qt.SolidPattern)
            qp.fillRect(custom_option.rect, brush )
        
        if index.row() < lastRow and index.column() < lastCol:
            qp.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight())
            qp.drawLine(opt.rect.topRight(), opt.rect.bottomRight())
        elif index.row() == lastRow and index.column() == lastCol:
            pass
        elif index.row() == lastRow:
            qp.drawLine(opt.rect.topRight(), opt.rect.bottomRight())
        elif index.column() == lastCol:
            qp.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight())



        
        qp.restore()
        super().paint(qp, custom_option, index)
        
if __name__ == "__main__":
    program = Run_me()

如果我设置

os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
,那么在 QTableWidget 中,网格线和 QHeaderViews 边框线之间的线对齐错误。

自然网格线系统也会发生这种情况。

我尝试用

QStyledItemDelegate
修复它,但没有成功。

有人有经验吗?

截图:

python pyqt5 qtablewidget screen-size
1个回答
0
投票
class GridDelegate(QtWidgets.QStyledItemDelegate):
    pen = QtGui.QPen(QtGui.QColor('#000000'), 1)

    def paint(self, qp, opt, index):
        qp.save()
        custom_option = QtWidgets.QStyleOptionViewItem(opt)
        custom_option.state &= ~QtWidgets.QStyle.State_Selected
        qp.setPen(self.pen)

        lastRow = index.model().rowCount() - 1
        lastCol = index.model().columnCount() - 1


        if not opt.state & QtWidgets.QStyle.State_Selected:
            brush = QtGui.QBrush(QtGui.QColor("#ffffff"),QtCore.Qt.SolidPattern)
            qp.fillRect(custom_option.rect, brush )
            pass
        else:
            brush = QtGui.QBrush(QtGui.QColor("#1182dc"),QtCore.Qt.SolidPattern)
            qp.fillRect(custom_option.rect, brush )
        
        if index.row() < lastRow and index.column() < lastCol:
            qp.drawLine(opt.rect.bottomLeft()+QtCore.QPointF(0,0.5), opt.rect.bottomRight()+QtCore.QPointF(0.5,0.5))
            qp.drawLine(opt.rect.topRight()+QtCore.QPointF(0.5,0), opt.rect.bottomRight()+QtCore.QPointF(0.5,0.5))
        elif index.row() == lastRow and index.column() == lastCol:
            pass
        elif index.row() == lastRow:
            qp.drawLine(opt.rect.topRight()+QtCore.QPointF(0.5,0), opt.rect.bottomRight()+QtCore.QPointF(0.5,0.5))
        elif index.column() == lastCol:
            qp.drawLine(opt.rect.bottomLeft()+QtCore.QPointF(0,0.5), opt.rect.bottomRight()+QtCore.QPointF(0.5,0.5))

        
        qp.restore()
        super().paint(qp, custom_option, index)

结果:

我觉得这样调整后,效果会好很多。

© www.soinside.com 2019 - 2024. All rights reserved.