tableWidget.horizontalHeader().setStretchLastSection(True) forces Horizontal Scrollbar disappearance when window resizes

huangapple go评论90阅读模式
英文:

tableWidget.horizontalHeader().setStretchLastSection(True) forces Horizontal Scrollbar disappearance when window resizes

问题

以下是翻译的代码部分:

# 创建QTableWidget并设置参数
def fill_table(self):
    self.tableWidget = QTableWidget(self.view_frame)
    self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
    self.tableWidget.setAlternatingRowColors(True)
    self.tableWidget.setFocusPolicy(Qt.NoFocus)
    self.tableWidget.setShowGrid(False)
    self.tableWidget.setSortingEnabled(True)

    self.tableWidget.horizontalHeader().setHighlightSections(False)
    self.tableWidget.horizontalHeader().setProperty("showSortIndicator", True)
    self.tableWidget.horizontalHeader().setStretchLastSection(True)

    self.tableWidget.verticalHeader().setVisible(False)
    self.tableWidget.verticalHeader().setHighlightSections(False)
    self.ui.tableWidget.setRowCount(0)
    self.ui.tableWidget.setColumnCount(6)
    self.ui.tableWidget.setRowCount(len(self.zipped_log))
    self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
    self.ui.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
    self.ui.tableWidget.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
    self.ui.tableWidget.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
    self.ui.tableWidget.setFocusPolicy(Qt.NoFocus)

    # 填充表格内容
    for i in range(len(self.zipped_log)):
        self.ui.tableWidget.setItem(i, 0, QTableWidgetItem(str(self.zipped_log[i]['date'])))
        self.ui.tableWidget.setItem(i, 1, QTableWidgetItem(str(self.zipped_log[i]['time'])))
        self.ui.tableWidget.setItem(i, 2, QTableWidgetItem(self.zipped_log[i]['source']))
        self.ui.tableWidget.setItem(i, 3, QTableWidgetItem(self.zipped_log[i]['code']))
        self.ui.tableWidget.setItem(i, 4, QTableWidgetItem(str(self.zipped_log[i]['severity'])))
        self.ui.tableWidget.setItem(i, 5, QTableWidgetItem(self.zipped_log[i]['description']))

    self.ui.tableWidget.resizeColumnsToContents()
    self.ui.tableWidget.resizeRowsToContents()
    self.ui.tableWidget.horizontalHeader().setStretchLastSection(True)

# 处理窗口大小变化事件
def resizeEvent(self, event):
    self.ui.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    self.ui.tableWidget.setWordWrap(False)
    QMainWindow.resizeEvent(self, event)

如果您需要关于解决问题的建议,请随时提出。

英文:

I've created a QTableWidget with these params.

def fill_table(self):
self.tableWidget = QTableWidget(self.view_frame)
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.setAlternatingRowColors(True)
self.tableWidget.setFocusPolicy(Qt.NoFocus)
self.tableWidget.setShowGrid(False)
self.tableWidget.setSortingEnabled(True)
self.tableWidget.horizontalHeader().setHighlightSections(False)
self.tableWidget.horizontalHeader().setProperty("showSortIndicator", True)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.verticalHeader().setVisible(False)
self.tableWidget.verticalHeader().setHighlightSections(False)
self.ui.tableWidget.setRowCount(0)
self.ui.tableWidget.setColumnCount(6)
self.ui.tableWidget.setRowCount(len(self.zipped_log))
self.ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
self.ui.tableWidget.setSelectionMode(QAbstractItemView.SingleSelection)
self.ui.tableWidget.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
self.ui.tableWidget.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
self.ui.tableWidget.setFocusPolicy(Qt.NoFocus)
for i in range(len(self.zipped_log)):
self.ui.tableWidget.setItem(i, 0, QTableWidgetItem(str(self.zipped_log[i]['date'])))
self.ui.tableWidget.setItem(i, 1, QTableWidgetItem(str(self.zipped_log[i]['time'])))
self.ui.tableWidget.setItem(i, 2, QTableWidgetItem(self.zipped_log[i]['source']))
self.ui.tableWidget.setItem(i, 3, QTableWidgetItem(self.zipped_log[i]['code']))
self.ui.tableWidget.setItem(i, 4, QTableWidgetItem(str(self.zipped_log[i]['severity'])))
self.ui.tableWidget.setItem(i, 5, QTableWidgetItem(self.zipped_log[i]['description']))
self.ui.tableWidget.resizeColumnsToContents()
self.ui.tableWidget.resizeRowsToContents()
self.ui.tableWidget.horizontalHeader().setStretchLastSection(True)
def resizeEvent(self, event):
self.ui.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.ui.tableWidget.setWordWrap(False)
QMainWindow.resizeEvent(self, event)

All works well, except filling the table with different content and resizing the window.

In first step i fill the table with short rows, which suit the size of the window. If i call filling function again, with long rows, which doesn't suit the size of the window, these rows will activate the horizontal scrollbar and all works well, i can scroll them horizontally. But long rows start wordwrapping after resizing the window, and i can't scroll them with horizontal scrollbar anymore.

It looks like three dots ... at the not actual end of the row but at the edge of the window.

I've already tried to use resizeEvent, but it didn't help.

How can i fix this problem? I want a dynamic horizontal ScrollBar, which will show as needed and rows will not be wordwrapped after resizing the window

答案1

得分: 1

setStretchLastSection() 方法会将最后一列的调整模式更改为 QHeaderView.Stretch。在 Stretch 模式下,无论列的内容是否比它宽,该列始终填充可用空间,因此不会出现水平滚动条。(列的大小不会根据列的内容自动调整。)

因此,如果您使用 QTableView.resizeColumnsToContents(),我建议不要使用 setStretchLastSection()

如果您不介意以一种不太正规的方式手动调整列大小,您可以这样做。(这不太正规,因为在将来的 Qt 版本中可能会失效。)

from PyQt5.QtCore import QTimer
...

def resize_tableview_columns(tableview):
    tableview.resizeColumnsToContents()
    def on_idle():
        if not tableview.horizontalScrollBar().isVisible():
            header = tableview.horizontalHeader()
            last_col = header.count() - 1
            header.resizeSection(last_col, tableview.viewport().width() -
                sum(header.sectionSize(i) for i in range(last_col)))
    QTimer.singleShot(0, on_idle)

...

def fill_table(self):
    self.tableWidget = QTableWidget(self.view_frame)
    ...
    #self.tableWidget.horizontalHeader().setStretchLastSection(True) # 不要这样做。
    ...
    #self.ui.tableWidget.resizeColumnsToContents() # 使用下面的代替这个。
    resize_tableview_columns(self.ui.tableWidget)
    ...
    #self.ui.tableWidget.horizontalHeader().setStretchLastSection(True) # 不要这样做。

希望这对您有所帮助。

英文:

The setStretchLastSection() changes the resize mode of the last section to the QHeaderView.Stretch. In the Stretch mode, the section always fills the available space whether contents of the column are wider than it or not, so the horizontal scrollbar will not appear. (The section size will not grow to fit contents of the column.)

So, I recommend not using the setStretchLastSection() if you use the QTableView.resizeColumnsToContents().

If you don't mind resizing columns manually in a hacky way, you can do this. (It's hacky because it may be broken in a future Qt version.)

from PyQt5.QtCore import QTimer
...
def resize_tableview_columns(tableview):
tableview.resizeColumnsToContents()
def on_idle():
if not tableview.horizontalScrollBar().isVisible():
header = tableview.horizontalHeader()
last_col = header.count() - 1
header.resizeSection(last_col, tableview.viewport().width() -
sum(header.sectionSize(i) for i in range(last_col)))
QTimer.singleShot(0, on_idle)
...
def fill_table(self):
self.tableWidget = QTableWidget(self.view_frame)
...
#self.tableWidget.horizontalHeader().setStretchLastSection(True) # Don't do this.
...
#self.ui.tableWidget.resizeColumnsToContents() # Use the below instead of this.
resize_tableview_columns(self.ui.tableWidget)
...
#self.ui.tableWidget.horizontalHeader().setStretchLastSection(True) # Don't do this.

答案2

得分: 0

The main problem was that the table remembered the longest row length and used it even with a different file with shorter rows. So, I just update the last section every time I refill the table with the contents.
I've solved the problem like this:

在MainClass中:

def resizeEvent(self, event):
    self.ui.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    self.ui.tableWidget.setWordWrap(False)
    QMainWindow.resizeEvent(self, event)

def fill_table(self):
    self.tableWidget.horizontalHeader().setStretchLastSection(False)
    # 填充表格的代码
    self.tableWidget.resizeColumnsToContents()
    self.tableWidget.resizeRowsToContents()
    self.tableWidget.horizontalHeader().setStretchLastSection(True)

(Note: The code snippet provided is a translation of the code portion you provided, excluding any additional content.)

英文:

The main problem was that table remembered the longest row length and used it even with different file with shorter rows. So, i just update the last section every time i refill the table with the contents.
I've solved the problem like that:

in MainClass:

def resizeEvent(self, event):
self.ui.tableWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.ui.tableWidget.setWordWrap(False)
QMainWindow.resizeEvent(self, event)
def fill_table(self):
self.tableWidget.horizontalHeader().setStretchLastSection(False)
...
filling the table
...
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.horizontalHeader().setStretchLastSection(True)

huangapple
  • 本文由 发表于 2023年5月29日 22:57:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定