What I want to get:

I am trying to layout a QTableWidget with last Column being a text, that should be word wrapped, so that it is easily readable without horizontal scrolling. The row height should be adjusted, so that the QTableWidget will show only one vertical Slidebar for the whole widget.


What I get:

The default Layout of the QTableWidget uses a horizontal slider for a very long and unwrapped QTextEdit. If I adjust the width and height of the QTextEdit manually it is the way I want it.

I tried it many ways without a real progress. Maybe somebody can point me the right direction... Am not even sure if the use of QTextEdit is appropriate.



What I got:

Qt Code:
  1. #
  2. # Excerpt output from pyuic
  3. #
  4. self.History = QtGui.QTableWidget(self.groupBox_9)
  5. self.History.setAlternatingRowColors(True)
  6. self.History.setSelectionBehavior(QtGui.QAbstractItemView.SelectItems)
  7. self.History.setTextElideMode(QtCore.Qt.ElideNone)
  8. self.History.setWordWrap(True)
  9. self.History.setObjectName("History")
  10. self.History.setColumnCount(3)
  11. self.History.setRowCount(0)
  12. item = QtGui.QTableWidgetItem()
  13. self.History.setHorizontalHeaderItem(0, item)
  14. item = QtGui.QTableWidgetItem()
  15. self.History.setHorizontalHeaderItem(1, item)
  16. item = QtGui.QTableWidgetItem()
  17. self.History.setHorizontalHeaderItem(2, item)
  18. self.verticalLayout_10.addWidget(self.History)
  19.  
  20.  
  21. #
  22. # from the Databaseviewer
  23. #
  24. widget = self.ui.History
  25. widget.clearContents()
  26. # read the story from database
  27. story = self.getHistory()
  28. widget.setRowCount(len(story))
  29. remember_sorting = widget.isSortingEnabled()
  30. widget.setSortingEnabled(False)
  31. for index_line, items in enumerate(story):
  32. id, first_created, last_updated, memo, remind = items
  33. widg1 = (first_created and first_created.strftime("%Y%m%d %H:%M") ) or ""
  34. widg2 = (remind and remind.strftime("%Y%m%d %H:%M") ) or ""
  35. widg3 = memo or ""
  36. for index_col, widgetitem in enumerate( (widg1, widg2, widg3) ):
  37. it = QtCore.QString( unicode( widgetitem ) )
  38. it = QtGui.QTableWidgetItem( it )
  39. it.setData(QtCore.Qt.UserRole, QtCore.QVariant(id))
  40. widget.setItem(index_line , index_col, it)
  41. if index_col == 2:
  42. pol = QtGui.QSizePolicy( QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Expanding )
  43. pol.setHeightForWidth(True)
  44. pol.setVerticalStretch( 9999 )
  45. it = QtGui.QTextEdit()
  46. it.setWordWrapMode( QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere )
  47. it.setLineWrapMode( QtGui.QTextEdit.WidgetWidth )
  48. it.setSizePolicy( pol )
  49. it.setPlainText( QtCore.QString( unicode( widgetitem )))
  50. print it.sizePolicy().horizontalPolicy(), it.sizePolicy().verticalPolicy()
  51. print it.sizePolicy().horizontalStretch(), it.sizePolicy().verticalStretch()
  52. widget.setCellWidget( index_line, index_col, it )
  53. else:
  54. pass
  55. widget.resizeColumnsToContents()
  56. widget.resizeRowsToContents()
To copy to clipboard, switch view to plain text mode