Sure, here's a simple version of my class (I removed the unneeded stuff for this problem, the following code reproduces the problem):
sizeHint():
QSize MyDelegate
::sizeHint(const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
// Override the default height
if(fm.height() > 16)
{
size.setHeight(fm.height() + 4);
}
else
{
size.setHeight(20);
}
//return QSize(1000,50);
return size;
}
QSize MyDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
// Override the default height
QSize size(QItemDelegate::sizeHint(option, index));
QFontMetrics fm(option.fontMetrics);
if(fm.height() > 16)
{
size.setHeight(fm.height() + 4);
}
else
{
size.setHeight(20);
}
//return QSize(1000,50);
return size;
}
To copy to clipboard, switch view to plain text mode
paint():
void MyDelegate
::paint(QPainter* painter,
const QStyleOptionViewItem
& option,
const QModelIndex
& index
) const {
painter->save();
// Useless stuff skipped...
// Draw the content of the delegate
// Text rectangle
// The text will be vertically centered.
// If a context is available, the context will be displayed in bold, between brackets.
QString text
(index.
data(Qt
::DisplayRole).
value<QString>
());
if(!index.parent().isValid()) // TODO: IF CONTEXT
{
text.append(" <b>(bold text)</b>"); // TODO: get the text from the model
}
textDoc.setHtml(text);
int textX = option.rect.x();
int textY = (option.rect.height() - textDoc.size().height())/2 + option.rect.y();
// Set the text rectangle width (the smaller of the two will be used): either as wide as the text width, or as
int textWidth = qMin<int>(textDoc.size().width(), option.rect.width());
QRect textRect
(textX, textY, textWidth, option.
rect.
height());
// Translate the painter to the position of the text rectangle in order to paint the QTextDocument
// Then paint the text as HTML formatted text
painter->save();
painter->translate(textRect.topLeft());
textDoc.drawContents(painter, textRect.translated(-textRect.topLeft()));
painter->restore();
painter->restore();
}
void MyDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
painter->save();
// Useless stuff skipped...
// Draw the content of the delegate
// Text rectangle
// The text will be vertically centered.
// If a context is available, the context will be displayed in bold, between brackets.
QString text(index.data(Qt::DisplayRole).value<QString>());
if(!index.parent().isValid()) // TODO: IF CONTEXT
{
text.append(" <b>(bold text)</b>"); // TODO: get the text from the model
}
QTextDocument textDoc;
textDoc.setHtml(text);
int textX = option.rect.x();
int textY = (option.rect.height() - textDoc.size().height())/2 + option.rect.y();
// Set the text rectangle width (the smaller of the two will be used): either as wide as the text width, or as
int textWidth = qMin<int>(textDoc.size().width(), option.rect.width());
QRect textRect(textX, textY, textWidth, option.rect.height());
// Translate the painter to the position of the text rectangle in order to paint the QTextDocument
// Then paint the text as HTML formatted text
painter->save();
painter->translate(textRect.topLeft());
textDoc.drawContents(painter, textRect.translated(-textRect.topLeft()));
painter->restore();
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks