Some reason option.rect.width() returns 0 in QSize StatusWidgetDelegate::sizeHint()
Seems in paint() width is set fine. So I am potentially able to draw it correctly but I cannot set correct size of the item.
Added after 22 minutes:
So this is what I came up with and works how I want. But some reason I have to set
item
->setSizeHint
(QSize(this
->width
(),
10));
item->setSizeHint(QSize(this->width(), 10));
To copy to clipboard, switch view to plain text mode
during insert an item to give a width for &options in QSize StatusWidgetDelegate::sizeHint(). Well, this is fine for me, but I don't like any fake information like a height = 10 ( this could be whatever value because its quickly overwritten by sizeHint() logic). Also in this example I need to use resizeEvent() to update the width of the all items. This would be not needed if &option in sizeHint() would return correct rect.
So what I am doing wrong ?
QListView
void List::insert(Status &status)
{
int row = model()->rowCount();
item
->setSizeHint
(QSize(this
->width
(),
10));
item->setData(status.message(), StatusWidgetDelegate::MessageRole);
model()->insertRow(row,item);
}
void List::insert(Status &status)
{
int row = model()->rowCount();
QStandardItem *item = new QStandardItem;
item->setSizeHint(QSize(this->width(), 10));
item->setData(status.message(), StatusWidgetDelegate::MessageRole);
model()->insertRow(row,item);
}
To copy to clipboard, switch view to plain text mode
{
for( int i = 0; i < model()->rowCount(); i++)
{
model
()->item
(i
)->setSizeHint
(QSize(this
->width
(),
10));
}
}
void List::resizeEvent(QResizeEvent *e)
{
for( int i = 0; i < model()->rowCount(); i++)
{
model()->item(i)->setSizeHint(QSize(this->width(),10));
}
}
To copy to clipboard, switch view to plain text mode
QStyledItemDelegate
StatusWidgetDelegate
::StatusWidgetDelegate(QObject *parent
) : QStyledItemDelegate(parent), m_currentItemHeight(0)
{
}
{
QString text
= index.
data(MessageRole
).
toString();
m_currentItemHeight = fm.boundingRect(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text).height();
QSize size
= QStyledItemDelegate
::sizeHint(option, index
);
size.setHeight(m_currentItemHeight);
size.setWidth(size.width() - 10);
return size;
}
{
QString text
= index.
data(MessageRole
).
toString();
painter->save();
painter->setPen(Qt::NoPen);
painter
->setBrush
(QColor(220,
220,
250));
painter->drawRect(r);
painter->restore();
painter->drawText(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text);
}
StatusWidgetDelegate::StatusWidgetDelegate(QObject *parent) :
QStyledItemDelegate(parent), m_currentItemHeight(0)
{
}
QSize StatusWidgetDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString text = index.data(MessageRole).toString();
QRectF r = option.rect;
QFontMetricsF fm(QApplication::font());
m_currentItemHeight = fm.boundingRect(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text).height();
QSize size = QStyledItemDelegate::sizeHint(option, index);
size.setHeight(m_currentItemHeight);
size.setWidth(size.width() - 10);
return size;
}
void StatusWidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QString text = index.data(MessageRole).toString();
QRectF r = option.rect;
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(220,220,250));
painter->drawRect(r);
painter->restore();
painter->drawText(r, Qt::AlignJustify | Qt::AlignVCenter | Qt::TextWordWrap, text);
}
To copy to clipboard, switch view to plain text mode
Thanks for looking...
Added after 47 minutes:
The other thing is that a QListView scroll bar does not react on new delegates. Moving Bottom Window up to resize a height of the entire mainWindow does not show a QListView scroll bar. so long list items is behind and not visible.
Something is wrong here.
Bookmarks