[Qt4] Noob and custom Item Delegate
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:
Code:
{
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
);
}
here is my modelView data
Code:
{
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
{
}
}
Now i have no idea how to make my delegate paint icon and text (in one column). Any suggestion/help?
Re: [Qt4] Noob and custom Item Delegate
It's best to see how the default delegate does it. Where to get the text to render? From the model :) Using data(index, Qt :: DisplayRole). But in your case it should be enough to change style options (to turn on word wrapping) and call the default delegate.
Re: [Qt4] Noob and custom Item Delegate
Turn on word wrapping to what? QTreeView and QItemDelegate has no such property. I also need to display description italic... I know that QTreeView has property uniformRowHeights and its already changed to false (by calling setUniformRowHeights(FALSE)). So where to set word wrapping and is it possible to display description in italics?
Re: [Qt4] Noob and custom Item Delegate
You might want to take a look at QStyleOptionViewItem class. You have the ability to change font settings there.
Looks like I might have been wrong with the word wrapping thing, you need to set it directly.
Anyway, this is my multiline drawing "thing" (it's not a delegate but rather part of the view, but all rules remain the same).
Code:
void ColumnChartView
::drawRowNames( QPainter * p
) { int itemwidth = itemWidth();
int seriesCount = model()->columnCount();
int columnWidth = itemWidth()/seriesCount;
int maxh = chartSize().height();
int valueSpan = _maxVal - _minVal;
uint rowHeight
= (_flags
& XTitle
) ? _canvasVMargin
-QFontMetrics(font
()).
height()-2 : _canvasVMargin;
p->save();
for(int i=0;i<model()->rowCount(rootIndex());i++) {
QRect rect
(qRound
(_canvasHMargin
+itemwidth
/4+i
*itemwidth
*3/2), maxh
+_canvasVMargin, itemwidth, rowHeight
);
p->setFont(opt.font);
p
->drawText
(rect,opt.
displayAlignment|Qt
::TextWordWrap,
QAbstractItemDelegate::elidedText(fontMetrics
(), rect.
width(),Qt
::ElideRight,model
()->headerData
(i, Qt
::Vertical).
toString()));
}
p->restore();
}
In your case you need to use model()->data() instead of model()->headerData().
Just for completeness:
Code:
opt.displayAlignment |= Qt::AlignHCenter;
opt.displayAlignment &= ~Qt::AlignVCenter;
opt.displayAlignment |= Qt::AlignTop;
value = model()->headerData(row, Qt::Vertical, Qt::FontRole);
if(value.isValid())
opt.font = qvariant_cast<QFont>(value);
value = model()->headerData(row, Qt::Vertical, Qt::TextColorRole);
if(value.isValid() && qvariant_cast<QColor>(value).isValid())
opt.
palette.
setColor(QPalette::Text, qvariant_cast<QColor>
(value
));
return opt;
}
Re: [Qt4] Noob and custom Item Delegate
And when should i call drawRowNames?
Re: [Qt4] Noob and custom Item Delegate
You shouldn't :) I told you, it is part of my own code which does something different. But it behaves the same as the painting routine from a delegate. The part you might find interesting is the way to call drawText() and a way to set and use wanted item options (this is the way to transfer data between the view and the delegate -- for data transfer between the model and the delegate model()->data() should be used).
What my code does is to render a multiline text (if it can be wrapped to fit into the desired rectangle) or elide it if it's not possible to fit it all.
You might also want to reimplement sizeHint() for the delegate and calculate the size needed to fit your item (using QFontMetrics).
Re: [Qt4] Noob and custom Item Delegate
Well I've updated my delegate paint routine and still result is blank... Here it is:
Code:
{
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));
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=Qxygen->viewWidth(), height;
if(item->data(1).isEmpty())
{
height=fmetrics.height();
}
else
{
height=fmetrics.height()+fmetrics.height()*ceil(fmetrics.width(item->data(1))/width);
}
QRect rect
(option.
rect.
x(), option.
rect.
y(), width, height
);
painter
->drawText
(rect,Qt
::TextWordWrap,
QAbstractItemDelegate::elidedText(static_cast<const QFontMetrics
&>
(Qxygen
->roster
()->fontMetrics
()), rect.
width(),Qt
::ElideRight,Qxygen
->model
()->data
(index, Qt
::DisplayRole).
toString()));
printf("Width: %d Height:%d\n", width, height);
}
Still no idea why it doesn't print anything. As i said before my QTreeView behaves like there were all items... The scroll bar shows when i resize whole app :confused:
Re: [Qt4] Noob and custom Item Delegate
Code:
painter->setPen(Qt::NoPen);
How do you expect to paint anything without a pen? :)
Re: [Qt4] Noob and custom Item Delegate
Ok i've made even painting of icon, but it doesn't paint with the height i requested :|
Here's code:
Code:
{
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));
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=Qxygen->viewWidth(), height;
if(item->data(1).isEmpty())
{
height=fmetrics.height();
}
else
{
height=fmetrics.height()+fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=14;
}
printf("Width: %d Height:%d\n", width, height);
QImage img
=Qxygen
->model
()->data
(index, Qt
::DecorationRole).
value<QImage>
();
QRect irect
(option.
rect.
x(), option.
rect.
y(), img.
width(), img.
height());
QRect rect
(option.
rect.
x()+img.
width()+2, option.
rect.
y(), width
-img.
width()-2, height
);
opt.setAlignment(Qt::AlignTop|Qt::AlignLeft);
painter->drawImage(irect, img);
// painter->drawText(rect, Qxygen->model()->data(index, Qt::DisplayRole).toString(), opt);
painter->drawText(rect, Qt::TextWordWrap|Qt::AlignLeft|Qt::AlignTop, Qxygen->model()->data(index, Qt::DisplayRole).toString());
// painter->drawText(rect,Qt::TextWordWrap,elidedText(static_cast<const QFontMetrics&>(Qxygen->roster()->fontMetrics()), rect.width(),Qt::ElideRight,Qxygen->model()->data(index, Qt::DisplayRole).toString()));
}
The painted text is in one line... When I resize app (height is calculated well i can see on console output) text isnt wrapped its only "hidden" behind border... Is there any possibility to put there rich text?
Re: [Qt4] Noob and custom Item Delegate
Quote:
Originally Posted by naresh
Ok i've made even painting of icon, but it doesn't paint with the height i requested :|
You can't "request" height. paint() only paints an item, it doesn't "set" its height. If you don't provide a boundingRect (or don't enable clipping) the text (or whatever you draw) will "flow out" of the item rectangle. Did you reimplement sizeHint for the delegate as I suggested (QItemDelegate)?
Re: [Qt4] Noob and custom Item Delegate
I didnt reimplement sizeHint... can you show me an example? I'm calculating width and height in paint (lines 20-29). Would you mind explaining me what were you mean by "don't enable clipping" and "boundingRect" ? ? ?
Re: [Qt4] Noob and custom Item Delegate
Quote:
Originally Posted by naresh
I didnt reimplement sizeHint... can you show me an example?
Take a look at QItemDelegate::sizeHint.
Quote:
Would you mind explaining me what were you mean by "don't enable clipping"
QPainter::setClipping (see "Clipping").
Quote:
and "boundingRect" ? ? ?
From QPainter::drawText:
Quote:
The boundingRect (if not null) is set to the actual bounding rectangle of the output.
and QPainter::boundingRect.
BTW. sizeHint item role may come in handy too. Maybe it's enough to make an item larger, you might want to try it.
Re: [Qt4] Noob and custom Item Delegate
reimplementing sizeHint helped but not as i expected... It makes visible only 2 lines of text, (so it's viewing minimal size about 28px for items with description), then if i resize other words are cut... here is code:
Code:
{
painter
->setRenderHint
(QPainter::TextAntialiasing);
if (option.
state==QStyle::State_Selected) painter->setBrush(Qxygen->roster()->palette().highlight());
else
painter
->setBrush
(QBrush(Qt
::white));
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=Qxygen->viewWidth(), height;
QImage img
=Qxygen
->model
()->data
(index, Qt
::DecorationRole).
value<QImage>
();
QRect irect
(option.
rect.
x(), option.
rect.
y()+2, img.
width(), img.
height());
QRect nrect
(option.
rect.
x()+img.
width()+2, option.
rect.
y()+2, width
-img.
width()-2, fmetrics.
height());
painter->drawImage(irect, img);
// CONTACT NAME
painter->drawText(nrect, Qt::AlignLeft|Qt::AlignTop, elidedText(static_cast<const QFontMetrics&>(Qxygen->roster()->fontMetrics()), nrect.width(),Qt::ElideRight,item->data(0)));
if(!item->data(1).isEmpty())
{
int height=fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
QRect drect
(option.
rect.
x(), option.
rect.
y()+img.
height()+2, width, height
);
// DESCRIPTION
painter->drawText(drect, Qt::TextWordWrap|Qt::AlignLeft|Qt::AlignTop, item->data(1));
}
}
{
rosterItem *item = static_cast<rosterItem*>(index.internalPointer());
int width=Qxygen->viewWidth(), height;
QImage img
(Qxygen
->model
()->data
(index, Qt
::DecorationRole).
value<QImage>
());
if(item->data(1).isEmpty())
{
height=img.height()+2;
}
else
{
height=img.height()+2+fmetrics.height()*qRound(fmetrics.width(item->data(1))/width);
printf("%d %d\n", height, qRound(fmetrics.width(item->data(1))/width));
if(fmetrics.width(item->data(1))%width>0)
height+=fmetrics.height();
}
return QSize(width, height
+2);
}
also highlighting selected item doesnt work :| But i don't care about that
Any idea why it draws only 2 lines? boundingRect didn't help and setClipping didn't help too... After setting clipping to TRUE nothing was painted :|
Re: [Qt4] Noob and custom Item Delegate
Highlighting doesn't work because you ignore the style options used for that. Are you inheriting QAbstractItemDelegate or QItemDelegate?
As for the size hint -- try setting some (big) constant value and see if that helps.
Quote:
After setting clipping to TRUE nothing was painted
Did you set a rectangle to use for clipping? If not, then you probably "clipped out" the whole painter.
Re: [Qt4] Noob and custom Item Delegate
The reason for not painting all lines after resize is that painting isn't called after resize... I need to cath resize event, and write something like "repaintAll". And how to make highlight work? What do you mean "ignore the style options". Im inheriting QItemDelegate
3 Attachment(s)
Re: [Qt4] Noob and custom Item Delegate
In attachments there are screens showing how does it behave.
1. Everything is fine!
2. When resized empty line left
3. When collapase and expand group everything looks fine
Re: [Qt4] Noob and custom Item Delegate
Does the view ask the delegate for the sizeHint after resize? (use qDebug to check that out).
2 Attachment(s)
Re: [Qt4] Noob and custom Item Delegate
I have no idea how to check that :| (just call qDebug()?, where?)
I've added drawRect in paint and the result is like this (screens). It looks like the option.rect.x() and option.rect.y() doesn't change at all, only when collapse and expand group and it change only for items in collapsed group :|
1 Attachment(s)
Re: [Qt4] Noob and custom Item Delegate
Quote:
Originally Posted by naresh
I have no idea how to check that :| (just call qDebug()?, where?)
In your sizeHint implementation of the delegate subclass.
BTW. Did you try just using sizeHint role for your model?
Re: [Qt4] Noob and custom Item Delegate
Ok I've added before return in sizeHint something like this:
Code:
qDebug("w:%d h%d", width, height)
And in paint at end something like this:
Code:
Qxygen->model()->setData(index, sizeHint(option,index), Qt::SizeHintRole);
Everytime I'm resizing window the proper width and height is print on console for every item :|