Results 1 to 2 of 2

Thread: QTableWidget/View resizeColumnsToContents not respected until scroll (PyQt4)

  1. #1
    Join Date
    Jun 2012
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Question QTableWidget/View resizeColumnsToContents not respected until scroll (PyQt4)

    The columns of my table do not resize to contents until you scroll to an item requiring the column be expanded AND resize the splitter pane. Resizing the entire window has the same effect as it also resizes the splitter. My table requires persistent editors, and you will see at row 16 there is a table with 50 columns, which should...after calling 'resizeColumnsToContents' determine the width of column 0. However it will not resize until you scroll down to it and trigger a splitter resize.

    Qt Code:
    1. from PyQt4 import QtCore,QtGui
    2. import sys
    3.  
    4. app = QtGui.QApplication(sys.argv)
    5. mw = QtGui.QMainWindow()
    6. spl = QtGui.QSplitter()
    7. spl.addWidget(QtGui.QLabel('Filler'))
    8.  
    9. # create table
    10. class tbl(QtGui.QTableWidget):
    11. def __init__(self):
    12. super(tbl,self).__init__()
    13. self.setRowCount(20)
    14. self.setColumnCount(2)
    15. self.horizontalHeader().setStretchLastSection(False)
    16. self.horizontalHeader().setResizeMode(QtGui.QHeaderView.ResizeToContents)
    17. self.verticalHeader().setResizeMode(QtGui.QHeaderView.ResizeToContents)
    18. self.setItemDelegate(delg(self))
    19.  
    20. # persist editors
    21. for x in xrange(0,self.rowCount()):
    22. for y in xrange(0,self.columnCount()):
    23. self.setItem(x,y,QtGui.QTableWidgetItem('test'))
    24. for x in xrange(0,self.rowCount()):
    25. for y in xrange(0,self.columnCount()):
    26. self.openPersistentEditor(self.item(x,y))
    27. self.resizeColumnsToContents()
    28.  
    29. # create item delegate
    30. class delg(QtGui.QItemDelegate):
    31. def __init__(self,parent):
    32. super(delg,self).__init__(parent)
    33.  
    34. def createEditor(self,parent,option,index):
    35. if index.column() == 1:
    36. return QtGui.QLineEdit(parent=parent)
    37. if index.row() == 15:
    38. t = QtGui.QTableWidget(parent=parent)
    39. t.setRowCount(5)
    40. t.setColumnCount(50)
    41. return t
    42. return QtGui.QLineEdit(parent=parent)
    43.  
    44. t = tbl()
    45. spl.addWidget(t)
    46. mw.setCentralWidget(spl)
    47.  
    48. # add and show
    49. mw.show()
    50. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Is there a way to force the table to respect the size of all columns? I have tried overwriting the minimumSizeHint for the editor table but get quirky results, eg: the second column will be painted ontop of the editor table....until I resize the splitter which triggers something....a repaint?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QTableWidget/View resizeColumnsToContents not respected until scroll (PyQt4)

    You need to call resizeColumnsToContents() when every QTrewWidget is scrolled, and when every QTreeWidget has items added/removed.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    BreakBad (22nd April 2013)

Similar Threads

  1. QSortFilterProxyModel search and view scroll help
    By waynew in forum Qt Programming
    Replies: 1
    Last Post: 10th November 2011, 01:58
  2. pyqt4 reading values from rows of QTableWidget
    By stephen76 in forum Newbie
    Replies: 2
    Last Post: 10th January 2011, 09:55
  3. PyQt4 - Inserting data from array to QTableWidget
    By phosse1 in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2010, 20:20
  4. QTableWidget resizeColumnsToContents for whole table?
    By lalesculiviu in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2010, 00:08
  5. Scroll View for Canvas
    By Kapil in forum Qt Programming
    Replies: 10
    Last Post: 25th March 2006, 07:19

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.