gpt4 book ai didi

python - 如何在 pyqt 中向 Qtablewidget 添加布局?

转载 作者:行者123 更新时间:2023-12-01 03:27:11 24 4
gpt4 key购买 nike

我的 qtablewidget 定义如下:

def __init__(self, parent = None):
super(Window, self).__init__(parent)
QtGui.QWidget.__init__(self)
QtGui.QTableWidget.setMinimumSize(self, 500, 500)
QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
self.table = QtGui.QTableWidget()
rowf = 3
self.table.setColumnCount(3)
self.table.setRowCount(rowf)
self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
self.table.verticalHeader().hide()

header = self.table.horizontalHeader()
header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(1, QtGui.QHeaderView.ResizeToContents)
header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)

self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)

self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.table)
self.verticalLayout.addWidget(self.buttonBox)

self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)

我希望我的最终结果看起来类似于下图,但现在,我尝试添加的布局并没有按照我希望的方式安静地工作。我是 pyqt 的初学者。我之前在 qlistview 上尝试过这种布局,效果很好。

enter image description here

最佳答案

添加{您的表格}.table.horizo​​ntalHeader().setStretchLastSection(True)和/或{您的表格}.verticalHeader().setStretchLastSection(True)

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore


class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
QtGui.QTableWidget.setMinimumSize(self, 500, 500)
QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
self.table = QtGui.QTableWidget()
rowf = 3
self.table.setColumnCount(3)
self.table.setRowCount(rowf)
self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
self.table.horizontalHeader().setStretchLastSection(True)
# self.table.verticalHeader().setStretchLastSection(True)

self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)

self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.addWidget(self.table)
self.verticalLayout.addWidget(self.buttonBox)

self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(self.close)

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

仅水平:

enter image description here

仅垂直:

enter image description here

垂直和水平:

enter image description here

关于python - 如何在 pyqt 中向 Qtablewidget 添加布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41319407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com