PDA

View Full Version : Autoscale QTableWidget columns on resize



genjix
13th February 2011, 03:43
Say I add a bunch of columns to my TableWidget.


tw.horizontalHeader().resizeSection(0, 130)
tw.horizontalHeader().resizeSection(1, 150)
tw.horizontalHeader().resizeSection(2, 400)
tw.horizontalHeader().resizeSection(3, 100)
tw.horizontalHeader().resizeSection(4, 100)


How can it resize (keeping proportions) so you never get the empty space at the end, nor horizontal scrollbars?

5939

Thanks.

Lykurg
13th February 2011, 10:00
Have a look at QHeaderView::stretchLastSection(). That is maybe not exactly what you are looking for, but the easiest one. If not, you have to subclass your view (or install an event filter) and reimp QWidget::resizeEvent(). There you can set the width of your sections according the widget size.

OT: What font are you using in that application?

genjix
13th February 2011, 22:09
Thanks for your help. I will check those out.

That font is called 'Ubuntu' :) I can send you it if you find me on freenode #bitcoin-dev and message me (my nick is genjix)

Added after 1 46 minutes:

Just dumping this code here for others who may search for this on Google:

class TransactionsTable(QTableWidget):
def __init__(self):
super(TransactionsTable, self).__init__()
tw = self
tw.setColumnCount(5)
hedlabels = ('Status', 'Date', 'Transactions', 'Credits', 'Balance')
self.hedprops = (130, 150, 400, 100, 100)
tw.setHorizontalHeaderLabels(hedlabels)
for i, sz in enumerate(self.hedprops):
tw.horizontalHeader().resizeSection(i, sz)
tw.setSelectionBehavior(tw.SelectRows)
tw.setSelectionMode(tw.NoSelection)
tw.setFocusPolicy(Qt.NoFocus)
tw.setAlternatingRowColors(True)
tw.verticalHeader().hide()
tw.setShowGrid(False)
tw.setHorizontalScrollBarPolicy(Qt.ScrollBarAlways Off)
#tw.setFixedSize(870, 300)
#tw.horizontalHeader().setResizeMode(QHeaderView.S tretch)
self.horizontalHeader().setStretchLastSection(True )

def resizeEvent(self, event):
selfsz = event.size().width()
totalprops = sum(self.hedprops)
newszs = [sz * selfsz / totalprops for sz in self.hedprops]
for i, sz in enumerate(newszs):
self.horizontalHeader().resizeSection(i, sz)


(sorry it's messy, haven't cleaned it up yet :p)