Hello! I'm trying to create custom item delegate to view multiline text in QTreeView properly... So I've started trying to create my delegate... the result is that it doesn't draw anything (the scrollbar appear when i resize app window so items are there) :| Here is some code:
painting:
{
painter
->setRenderHint
(QPainter::TextAntialiasing);
painter->setPen(Qt::NoPen);
if (option.
state & QStyle::State_Selected) painter->setBrush(option.palette.highlight());
else
painter
->setBrush
(QBrush(Qt
::white));
QSize sizehint
=sizeHint
(option, index
);
int width=Qxygen->viewWidth();
int height=ceil((sizehint.width()*sizehint.height())/width);
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
painter
->drawText
(QRectF(option.
rect.
x(), option.
rect.
y(), width, height
),
/* FROM WHERE GET THAT TEXT? */, textOption
);
}
void rosterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->setRenderHint(QPainter::TextAntialiasing);
painter->setPen(Qt::NoPen);
if (option.state & QStyle::State_Selected)
painter->setBrush(option.palette.highlight());
else
painter->setBrush(QBrush(Qt::white));
QTextOption textOption;
textOption.setWrapMode(QTextOption::WordWrap);
QSize sizehint=sizeHint(option, index);
int width=Qxygen->viewWidth();
int height=ceil((sizehint.width()*sizehint.height())/width);
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
painter->drawText(QRectF(option.rect.x(), option.rect.y(), width, height), /* FROM WHERE GET THAT TEXT? */, textOption);
}
To copy to clipboard, switch view to plain text mode
here is my modelView data
{
if (!index.isValid())
if (role == Qt::DisplayRole)
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
if(descr && !(item->isGroup())) // CHECKS IF VIEW HAS TO SHOW ITEM DESCRIPTION AND ITEM ISN'T GROUP - GROUPS DOESN'T NEED DESCRIPTION
{
if((item->data(1)).isEmpty()) return item->data(index.column());
else return QString(item
->data
(index.
column()))+"\n"+QString(item
->data
(1));
}
else
{
return item->data(index.column());
}
}
else if(role == Qt::DecorationRole)
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
if(item->isGroup())
{
if(item->isExpanded())
{
return QIcon(":expanded.png");
}
else
{
return QIcon(":collapsed.png");
}
}
else
{
return QIcon(":dnd.png");
}
}
else
{
}
}
QVariant rosterView::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole)
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
if(descr && !(item->isGroup())) // CHECKS IF VIEW HAS TO SHOW ITEM DESCRIPTION AND ITEM ISN'T GROUP - GROUPS DOESN'T NEED DESCRIPTION
{
if((item->data(1)).isEmpty()) return item->data(index.column());
else return QString(item->data(index.column()))+"\n"+QString(item->data(1));
}
else
{
return item->data(index.column());
}
}
else if(role == Qt::DecorationRole)
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
if(item->isGroup())
{
if(item->isExpanded())
{
return QIcon(":expanded.png");
}
else
{
return QIcon(":collapsed.png");
}
}
else
{
return QIcon(":dnd.png");
}
}
else
{
return QVariant();
}
}
To copy to clipboard, switch view to plain text mode
Now i have no idea how to make my delegate paint icon and text (in one column). Any suggestion/help?
Bookmarks