手动执行此操作的一种方法是重新实现 所在changeEvent
的小部件QTableWidget
。否则,您可以使用该self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
选项在每次小部件更改其大小时自动拉伸每一列。这里有一个例子:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork
class myWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(myWindow, self).__init__(parent)
self.setWindowTitle("Table")
headers = [str(x) for x in range(6)]
self.table = QtGui.QTableWidget()
self.table.setColumnCount(len(headers))
self.table.setHorizontalHeaderLabels(headers)
self.table.setMinimumSize(450,400)
self.table.setAlternatingRowColors(True)
self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
self.setCentralWidget(self.table)
def changeEvent(self, event):
if event.type() == QtCore.QEvent.WindowStateChange:
print "The window state changed" # Here you can resize the columns manually
return super(myWindow, self).changeEvent(event)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('myWindow')
main = myWindow()
main.show()
sys.exit(app.exec_())