PyQt 设置行高创建大行

PyQt Set Row Height Creates A Large Row

在 PyQt 中,我试图创建一个 QTableWidget。这一切都很完美。但是,当我尝试调整行大小时,所有行都会调整大小,最后一行除外。我不想使用 setMaximumHeight,因为行数可能会有所不同。

这是我的代码:

self.table = QtWidgets.QTableWidget()

self.table.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)

self.table.setAlternatingRowColors(True)
self.table.setStyleSheet("alternate-background-color: rgb(240, 240, 240);background-color: rgb(231, 231, 231);border-width: 0px; border-style: solid")
self.table.horizontalHeader().setStyleSheet("::section{Background-color:rgb(240, 240, 240); border: none; height: 70px}")
self.table.setFrameStyle(QtWidgets.QFrame.NoFrame)

self.table.setColumnCount(3)
self.table.setRowCount(7)

self.table.verticalHeader().setVisible(False)
self.table.setShowGrid(False)
self.table.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.table.setMinimumWidth(690)

self.horizontalHeader = self.table.horizontalHeader()
self.horizontalHeader.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.horizontalHeader.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
self.horizontalHeader.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
self.horizontalHeader.setDefaultAlignment(Qt.AlignLeft)
self.horizontalHeader.setDefaultAlignment(Qt.AlignVCenter)

self.table.setHorizontalHeaderLabels(("Deck;Due;New").split(";"))

self.tableHeaderFont = self.table.horizontalHeader().font()
self.tableHeaderFont.setPointSize(10)
self.tableHeaderFont.setBold(True)
self.tableHeaderFont.setWeight(75)
self.table.horizontalHeader().setFont(self.tableHeaderFont)

self.verticalHeader = self.table.verticalHeader()
self.verticalHeader.setMinimumSectionSize(30)

for section in range(self.verticalHeader.count()):
    self.verticalHeader.resizeSection(section, 30)

这是我得到的结果

您可以尝试在顶部设置尺寸政策。这就是我所做的:

self.table.setSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Minimum)

总代码如下:

self.table = QtWidgets.QTableWidget()

self.table.setSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Minimum)

self.table.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.table.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)

self.table.setAlternatingRowColors(True)
self.table.setStyleSheet("alternate-background-color: rgb(240, 240, 240);background-color: rgb(231, 231, 231);border-width: 0px; border-style: solid")
self.table.horizontalHeader().setStyleSheet("::section{Background-color:rgb(240, 240, 240); border: none; height: 70px}")
self.table.setFrameStyle(QtWidgets.QFrame.NoFrame)

self.table.setColumnCount(3)
self.table.setRowCount(7)

self.verticalHeader = self.table.verticalHeader()
self.verticalHeader.setMinimumSectionSize(30)
self.table.resizeRowsToContents()

for section in range(self.verticalHeader.count()):
    self.verticalHeader.resizeSection(section, 30)

self.table.verticalHeader().setVisible(False)
self.table.setShowGrid(False)
self.table.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
self.table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.table.setMinimumWidth(690)

self.horizontalHeader = self.table.horizontalHeader()
self.horizontalHeader.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.horizontalHeader.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
self.horizontalHeader.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
self.horizontalHeader.setDefaultAlignment(Qt.AlignLeft)
self.horizontalHeader.setDefaultAlignment(Qt.AlignVCenter)

self.table.setHorizontalHeaderLabels(("Name;Place;New").split(";"))

self.tableHeaderFont = self.table.horizontalHeader().font()
self.tableHeaderFont.setPointSize(10)
self.tableHeaderFont.setBold(True)
self.tableHeaderFont.setWeight(75)
self.table.horizontalHeader().setFont(self.tableHeaderFont)