Reputation: 1759
i have a window that has a table widget in it. I am using python/ pyqt4. I want when the window is maximized, the columns of the table are also resized to make them wider. Here is part of my code. What can i add to achieve this?
self.table=QTableWidget()
self.tableLabel.setBuddy(self.tableLabel)
self.table.setColumnCount(len(headers))
self.table.setHorizontalHeaderLabels(headers)
self.table.setMinimumSize(450,400)
self.table.isFullScreen()
self.table.setAlternatingRowColors(True)
self.addButton=QPushButton("New")
layout=QGridLayout()
layout.addWidget(self.tableLabel,0,0)
layout.addWidget(self.table,1,0,5,6)
layout.addWidget(self.addButton,7,5)
self.setLayout(layout)
self.setWindowTitle("Table")
Upvotes: 1
Views: 2310
Reputation: 11
Use this code for table
self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
Upvotes: 0
Reputation:
One way of doing this manually would be reimplementing the changeEvent
of the widget in which the QTableWidget
is in. Otherwise you could use the self.table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
option to automatically stretch each column every time your widget changes it's size. An example here:
#!/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_())
Upvotes: 1