View Single Post
Old 03-08-2011, 12:06 PM   #3
meme
Sigil developer
meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.meme ought to be getting tired of karma fortunes by now.
 
Posts: 1,274
Karma: 1101600
Join Date: Jan 2011
Location: UK
Device: Kindle PW, K4 NT, K3, Kobo Touch
That got me closer, but I'm still not quite there. I can get the table to resize so the last column expands (and even one of the columns in the middle) so the table fills the space. But the column widths don't stay as I want them.

Below is example code - just run it under python and it will create a very simple table. If you drag to resize it you will see that the table expands to fill the window by making the last column larger. BUT if you shrink the table you will see that the width of the last column becomes smaller than either the header or the column data, which is not what I want. I'd like it to never get smaller than the width of the header/data - forcing a scrollbar to appear just like it does for the other columns. Clearly there must be a setting to tell it to allow a column to get bigger to fill the space, but not smaller than it started with.


Code:
import sys
from PyQt4.Qt import Qt, QWidget, QVBoxLayout, QApplication, QTableWidget, QTableWidgetItem, QDialog

class TableWidget(QTableWidget):
    def __init__(self, *args):
        QTableWidget.__init__(self, *args)
        self.setRowCount(1)
        self.setColumnCount(4)

        self.setItem(0, 0, QTableWidgetItem('Short'))
        self.setItem(0, 1, QTableWidgetItem('Short'))
        self.setItem(0, 2, QTableWidgetItem('Test very long entry in the box to see what happens to this one'))
        self.setItem(0, 3, QTableWidgetItem('Test very long entry in the box to see what happens to this one'))

        self.setHorizontalHeaderLabels(['A', 'Test of a long header but a short entry column', 'B', 'Test of a long header and a long entry column' ])

        h = self.horizontalHeader()
        h.setStretchLastSection(True)
        self.resizeColumnsToContents()

def table():

    w = QWidget()
    layout = QVBoxLayout(w)
    w.setLayout(layout)

    table = TableWidget(w)
    layout.addWidget(table)
    return w

app = QApplication(sys.argv)
dialog = QDialog()
v = QVBoxLayout(dialog)
v.addWidget(table())

dialog.exec_()
meme is offline   Reply With Quote