如果表格不可见,则使用滚动条时默认会冻结垂直和水平标题。我希望滚动也适用于标题,因此滚动时,它们始终不可见。
这是例子:
import string
import random
from typing import Optional
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QTableWidget, QWidget, QAbstractItemView, QHeaderView, QTableWidgetItem, QHBoxLayout, \
QApplication
class DataTable(QTableWidget):
# region Constructor
def __init__(self, parent: Optional[QWidget] = None):
super(DataTable, self).__init__(parent)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.setSelectionBehavior(QAbstractItemView.SelectItems)
self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.horizontalHeader().setTextElideMode(Qt.ElideMiddle)
for j in range(20):
if self.columnCount() <= j:
self.insertColumn(j)
for i in range(20):
if self.rowCount() <= i:
self.insertRow(i)
self.setItem(i, j, QTableWidgetItem('Hello'))
word = ''.join(random.choice(string.ascii_letters) for _ in range(6))
self.setHorizontalHeaderLabels([str(word) for i in range(self.columnCount())])
self.setVerticalHeaderLabels([str(word) for i in range(self.rowCount())])
if __name__ == '__main__':
app = QApplication()
widget = QWidget()
widget.resize(400, 600)
layout = QHBoxLayout(widget)
layout.addWidget(DataTable())
widget.show()
app.exec()
对于任何感兴趣的人,我下面的代码有效。所以我基本上将 QTableWidget 包装在 QScrollArea 中,禁用 QTableWidget 中的滚动条并更新最小大小。
import string
import random
from typing import Optional
from PySide6.QtCore import Qt, QSize
from PySide6.QtWidgets import QTableWidget, QWidget, QAbstractItemView, QHeaderView, QTableWidgetItem, QHBoxLayout, \
QApplication, QScrollArea
class DataTable(QScrollArea):
# region Constructor
def __init__(self, parent: Optional[QWidget] = None):
super(DataTable, self).__init__(parent)
self.setWidget(DataTableWidget(self))
self.setWidgetResizable(True)
class DataTableWidget(QTableWidget):
def __init__(self, parent: Optional[QWidget] = None):
super(DataTableWidget, self).__init__(parent)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.setSelectionBehavior(QAbstractItemView.SelectItems)
self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.horizontalHeader().setTextElideMode(Qt.ElideMiddle)
self.setSizeAdjustPolicy(QAbstractItemView.AdjustToContents)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
for j in range(20):
if self.columnCount() <= j:
self.insertColumn(j)
for i in range(20):
if self.rowCount() <= i:
self.insertRow(i)
self.setItem(i, j, QTableWidgetItem('Hello'))
word = ''.join(random.choice(string.ascii_letters) for _ in range(6))
self.setHorizontalHeaderLabels([str(word) for i in range(self.columnCount())])
self.setVerticalHeaderLabels([str(word) for i in range(self.rowCount())])
self.setMinimumSize(self.get_table_widget_size())
def get_table_widget_size(self):
w = self.verticalHeader().width() + 4 # +4 seems to be needed
for i in range(self.columnCount()):
w += self.columnWidth(i) # seems to include gridline (on my machine)
h = self.horizontalHeader().height() + 4
for i in range(self.rowCount()):
h += self.rowHeight(i)
return QSize(w, h)
if __name__ == '__main__':
app = QApplication()
widget = QWidget()
widget.resize(400, 600)
layout = QHBoxLayout(widget)
layout.addWidget(DataTable())
widget.show()
app.exec()