I am working with the QTableWidget component in PyQt4, and I cannot get the correct column sizes according to their respective header sizes.
Here is what the table layout should look like (without pipes, obviously):
Index | Long_Header | Longer_Header 1 | 102402 | 100 2 | 123123 | 2 3 | 454689 | 18
The code I'm working with looks something like this:
import sys from PyQt4.QtCore import QStringList, QString from PyQt4.QtGui import QApplication, QMainWindow, QSizePolicy from PyQt4.QtGui import QTableWidget, QTableWidgetItem def createTable(): table = QTableWidget(5, 3) table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) headers = QStringList() headers.append(QString("Index")) headers.append(QString("Long_Header")) headers.append(QString("Longer_Header")) table.setHorizontalHeaderLabels(headers) table.horizontalHeader().setStretchLastSection(True)
When the application runs, the first column is fairly wide, while the other columns are somewhat compressed.
Is there a way to force the table to fit according to the width of the header and not the data itself? Even better, is there a way to make each column width be the maximum width of data and header values?
Refresh . I tried calling resizeColumnsToContents() on a table, but the view becomes terribly distorted:
Python table http://img514.imageshack.us/img514/8633/tablef.png
** Update2 **: resizeColumnsToContents() works just fine, as long as it is called after , all cells and headers have been inserted into the table.
python pyqt4
bedwyr
source share