PDA

View Full Version : Layout long text in QTableWidget



Tamtam
13th February 2009, 15:09
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:




#
# Excerpt output from pyuic
#
self.History = QtGui.QTableWidget(self.groupBox_9)
self.History.setAlternatingRowColors(True)
self.History.setSelectionBehavior(QtGui.QAbstractI temView.SelectItems)
self.History.setTextElideMode(QtCore.Qt.ElideNone)
self.History.setWordWrap(True)
self.History.setObjectName("History")
self.History.setColumnCount(3)
self.History.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.History.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.History.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.History.setHorizontalHeaderItem(2, item)
self.verticalLayout_10.addWidget(self.History)


#
# from the Databaseviewer
#
widget = self.ui.History
widget.clearContents()
# read the story from database
story = self.getHistory()
widget.setRowCount(len(story))
remember_sorting = widget.isSortingEnabled()
widget.setSortingEnabled(False)
for index_line, items in enumerate(story):
id, first_created, last_updated, memo, remind = items
widg1 = (first_created and first_created.strftime("%Y%m%d %H:%M") ) or ""
widg2 = (remind and remind.strftime("%Y%m%d %H:%M") ) or ""
widg3 = memo or ""
for index_col, widgetitem in enumerate( (widg1, widg2, widg3) ):
it = QtCore.QString( unicode( widgetitem ) )
it = QtGui.QTableWidgetItem( it )
it.setData(QtCore.Qt.UserRole, QtCore.QVariant(id))
widget.setItem(index_line , index_col, it)
if index_col == 2:
pol = QtGui.QSizePolicy( QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Expanding )
pol.setHeightForWidth(True)
pol.setVerticalStretch( 9999 )
it = QtGui.QTextEdit()
it.setWordWrapMode( QtGui.QTextOption.WrapAtWordBoundaryOrAnywhere )
it.setLineWrapMode( QtGui.QTextEdit.WidgetWidth )
it.setSizePolicy( pol )
it.setPlainText( QtCore.QString( unicode( widgetitem )))
print it.sizePolicy().horizontalPolicy(), it.sizePolicy().verticalPolicy()
print it.sizePolicy().horizontalStretch(), it.sizePolicy().verticalStretch()
widget.setCellWidget( index_line, index_col, it )
else:
pass
widget.resizeColumnsToContents()
widget.resizeRowsToContents()