def sizeHint(self, option, index):
"""
size hint is being used to control text wrap of the definition.
All columns are allowed to text wrap, but only definition is
believed to need text wrapping.
"""
# set the sizeHint for all columns.
if index.isValid():
fm = option.fontMetrics
text = index.model().data(index)
document.setDefaultFont(option.font)
document.setPlainText(text.toString())
# change cell Width, height (One can add or subtract to change
# the relative dimension).
#return QtCore.QSize(QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).width() + 200,
# QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height() +200)
# Else the index is not valid and then return the base method.
else:
return super(ObjDelegate, self).sizeHint(option, index)
def paint(self, painter, option, index):
"""
This will allow for text wrapping. The paint method will paint the text.
"""
if index.isValid():
# This will highlight the selected cell.
if option.
state & QtGui.
QStyle.
State_Selected: painter.fillRect(option.rect, option.palette.highlight())
# Get the text from the model.
text = index.model().data(index)
# Initialize the Qtextdocument.
document.setPlainText(text.toString())
document.setDefaultFont(option.font)
# settextwidth.
document.setTextWidth(option.rect.width())
# Save the painter. Saves the current painter state.
# This is so that we can redraw it later.
painter.save()
painter.translate(option.rect.topLeft())
document.documentLayout().draw(painter, ctx)
painter.restore()
else:
return super(ObjDelegate, self).paint(painter, option, index)
def sizeHint(self, option, index):
"""
size hint is being used to control text wrap of the definition.
All columns are allowed to text wrap, but only definition is
believed to need text wrapping.
"""
# set the sizeHint for all columns.
if index.isValid():
fm = option.fontMetrics
text = index.model().data(index)
document = QtGui.QTextDocument()
document.setDefaultFont(option.font)
document.setPlainText(text.toString())
# change cell Width, height (One can add or subtract to change
# the relative dimension).
#return QtCore.QSize(QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).width() + 200,
# QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height() +200)
return QtCore.QSize(document.idealWidth(), QtSql.QSqlRelationalDelegate.sizeHint(self, option, index).height())
# Else the index is not valid and then return the base method.
else:
return super(ObjDelegate, self).sizeHint(option, index)
def paint(self, painter, option, index):
"""
This will allow for text wrapping. The paint method will paint the text.
"""
if index.isValid():
# This will highlight the selected cell.
if option.state & QtGui.QStyle.State_Selected:
painter.fillRect(option.rect, option.palette.highlight())
# Get the text from the model.
text = index.model().data(index)
# Initialize the Qtextdocument.
document = QtGui.QTextDocument()
document.setPlainText(text.toString())
document.setDefaultFont(option.font)
# settextwidth.
document.setTextWidth(option.rect.width())
ctx = QtGui.QAbstractTextDocumentLayout.PaintContext()
# Save the painter. Saves the current painter state.
# This is so that we can redraw it later.
painter.save()
painter.translate(option.rect.topLeft())
document.documentLayout().draw(painter, ctx)
painter.restore()
else:
return super(ObjDelegate, self).paint(painter, option, index)
To copy to clipboard, switch view to plain text mode
Bookmarks