[英]how to automatically expand a QTableWidget?
I have a problem concerning PyQt5 and widgets resizing in a QMdiSubWindow : the widget, a QTableWidget, should take all the width of the window, but it is only kept in the center, with a fixed width.我有一个关于 PyQt5 和小部件在 QMdiSubWindow 中调整大小的问题:小部件,一个 QTableWidget,应该占据窗口的所有宽度,但它只保留在中心,具有固定的宽度。
here is some code:这是一些代码:
class ProductDatasheetWindow(QMdiSubWindow):
def __init__(self, product):
super().__init__()
self.product = product
self.setWindowTitle("Liste produit")
main_layout = QVBoxLayout(self)
main_widget = QGroupBox()
main_widget.setLayout(main_layout)
main_widget.setSizePolicy(QSizePolicy.Maximum,QSizePolicy.Preferred)
self.setWidget(main_widget)
The VBoxLayout contains some infos (some QLabels) and at the bottom the QTableWidget: VBoxLayout 包含一些信息(一些 QLabels)和底部的 QTableWidget:
...table_attributs_valeurs = QTableWidget(len(product.attributes), 2,self)...
and here is some code I added in the constructor:这是我在构造函数中添加的一些代码:
table_attributs_valeurs.setContentsMargins(0, 0, 0, 0)
main_layout.setContentsMargins(0,0,0,0)
main_layout.addWidget(table_attributs_valeurs, 0, Qt.AlignJustify)
table_attributs_valeurs.setSizePolicy(QSizePolicy.Maximum,QSizePolicy.Preferred)
here is my minimal example, and writing it helped me to understand Qt and it works:这是我的最小示例,编写它帮助我理解 Qt 并且它有效:
from PyQt5.QtWidgets import QMdiSubWindow, QApplication, QVBoxLayout, QLabel, QWidget, QTableWidget, QTableWidgetItem
import sys
class Win(QMdiSubWindow):
def __init__(self):
QMdiSubWindow.__init__(self)
self.setMinimumSize(300,200)
main_widget=QWidget()
layout=QVBoxLayout(main_widget)
layout.addWidget(QLabel("numéro 1",self),0, Qt.AlignCenter)
layout.addWidget(QLabel("numéro 2",self), 0, Qt.AlignCenter)
main_widget.setLayout(layout)
self.setWidget(main_widget)
table=QTableWidget(3,2,self)
att_title=QTableWidgetItem("Attribut")
att_title.setTextAlignment(Qt.AlignCenter)
table.setHorizontalHeaderItem(0, att_title)
att_value=QTableWidgetItem("Valeur")
table.setHorizontalHeaderItem(1, att_value)
table.verticalHeader().setVisible(False)
header=table.horizontalHeader()
header.setSectionResizeMode(0,QtWidgets.QHeaderView.Stretch)
header.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
for row in range(3):
for col in range(2):
item=QTableWidgetItem("row {}, col {}".format(str(row),str(col)))
item.setTextAlignment(Qt.AlignCenter)
table.setItem(row,col,item)
layout.addWidget(table)
# layout.addStretch()
if __name__=="__main__":
app = QApplication(sys.argv)
win=Win()
win.show()
app.exec_()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.