Results 1 to 3 of 3

Thread: Autoscale QTableWidget columns on resize

  1. #1
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Autoscale QTableWidget columns on resize

    Say I add a bunch of columns to my TableWidget.
    Qt Code:
    1. tw.horizontalHeader().resizeSection(0, 130)
    2. tw.horizontalHeader().resizeSection(1, 150)
    3. tw.horizontalHeader().resizeSection(2, 400)
    4. tw.horizontalHeader().resizeSection(3, 100)
    5. tw.horizontalHeader().resizeSection(4, 100)
    To copy to clipboard, switch view to plain text mode 

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

    Screenshot.png

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Autoscale QTableWidget columns on resize

    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?

  3. #3
    Join Date
    Oct 2010
    Posts
    37
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Autoscale QTableWidget columns on resize

    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:
    Qt Code:
    1. class TransactionsTable(QTableWidget):
    2. def __init__(self):
    3. super(TransactionsTable, self).__init__()
    4. tw = self
    5. tw.setColumnCount(5)
    6. hedlabels = ('Status', 'Date', 'Transactions', 'Credits', 'Balance')
    7. self.hedprops = (130, 150, 400, 100, 100)
    8. tw.setHorizontalHeaderLabels(hedlabels)
    9. for i, sz in enumerate(self.hedprops):
    10. tw.horizontalHeader().resizeSection(i, sz)
    11. tw.setSelectionBehavior(tw.SelectRows)
    12. tw.setSelectionMode(tw.NoSelection)
    13. tw.setFocusPolicy(Qt.NoFocus)
    14. tw.setAlternatingRowColors(True)
    15. tw.verticalHeader().hide()
    16. tw.setShowGrid(False)
    17. tw.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
    18. #tw.setFixedSize(870, 300)
    19. #tw.horizontalHeader().setResizeMode(QHeaderView.Stretch)
    20. self.horizontalHeader().setStretchLastSection(True)
    21.  
    22. def resizeEvent(self, event):
    23. selfsz = event.size().width()
    24. totalprops = sum(self.hedprops)
    25. newszs = [sz * selfsz / totalprops for sz in self.hedprops]
    26. for i, sz in enumerate(newszs):
    27. self.horizontalHeader().resizeSection(i, sz)
    To copy to clipboard, switch view to plain text mode 

    (sorry it's messy, haven't cleaned it up yet )
    Last edited by genjix; 13th February 2011 at 21:09.

Similar Threads

  1. Don't resize columns - QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2009, 16:59
  2. Resizing QTableWidget Columns
    By mbrusati in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2008, 22:26
  3. QTableWidget - images in columns
    By bruccutler in forum Qt Programming
    Replies: 3
    Last Post: 1st May 2007, 18:28
  4. Resize QTreeWidget columns to contents
    By FaS in forum Qt Programming
    Replies: 9
    Last Post: 2nd July 2006, 20:48
  5. [qt4 & Xp] resize columns of a QTreeView
    By incapacitant in forum Newbie
    Replies: 4
    Last Post: 2nd March 2006, 14:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.