简体   繁体   English

PySide2,如何拉伸 QTableWidget 以适应 Window witdh?

[英]PySide2, how to stretch the QTableWidget to fit Window witdh?

I am trying to have a QTableWidget stretch horizontaly to fit the window width but I can't find how to do it.我试图让 QTableWidget 水平拉伸以适应窗口宽度,但我找不到如何去做。 I am new to Qt.我是 Qt 的新手。

The floowing code snippet and image show that, on resizing horizontaly the program window, the QLineEdit stretches to fit the window width but the QTableWidget doesn't.流动的代码片段和图像显示,在水平调整程序窗口的大小时,QLineEdit 会拉伸以适应窗口宽度,但 QTableWidget 不会。

在此处输入图片说明

import sys
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QTableWidget

app = QApplication(sys.argv)

win = QWidget()
win.setWindowTitle('test')
win.setMinimumWidth(400)

layV1 = QVBoxLayout()
win.setLayout(layV1)

entry = QLineEdit(win)
entry.setPlaceholderText('test entry widget')
layV1.addWidget(entry)

table = QTableWidget(win)
table.setRowCount(10)
table.setColumnCount(5)
layV1.addWidget(table)

win.show()
app.exec_()

You can use this to stretch the last section :您可以使用它来拉伸最后一部分:

table.horizontalHeader().setStretchLastSection(True) 

If you want to stretch a specific column, you need to use a QHeaderView.如果要拉伸特定列,则需要使用 QHeaderView。 Quick example with your code.您的代码的快速示例。

headerView = QHeaderView(QtCore.Qt.Horizontal, table)
table.setHorizontalHeader(headerView)
headerView.setSectionResizeMode(2, QHeaderView.Stretch)
headerView.setSectionsClickable(True)

Just replace 2 by the desired column to stretch!只需将 2 替换为所需的列即可拉伸!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM