fmetrics.height() is string height and fmetrics.width(...) is string width lines in qDebug output
fmetrics.height() is string height and fmetrics.width(...) is string width lines in qDebug output
Looks like options.rect is incorrect. Do you use a standard view? What model do you use?
Last edited by wysota; 14th March 2006 at 20:04.
I'm using standard QTreeView. I'm using my custom model based on QAbstractItemModel, and no i didn't reimplement visualRect in it. And here is new sizeHint... it seems to calculate better but painting is worse :|
Qt Code:
{ QRect tmp=option.fontMetrics.boundingRect(option.rect.x(), option.rect.y(), option.rect.width(), 1000, Qt::AlignLeft|Qt::AlignTop|Qt::TextWordWrap, index.model()->data(index, DescriptionRole).toString()); int height; if(tmp.height()) { height=img.height()+tmp.height()+2; } else { height=img.height()+2; } qDebug("%d %d", option.rect.width(), height); }To copy to clipboard, switch view to plain text mode
Could you explain "better" and "worse"?
Well better calculating means that size is now as i need (as i can see after calling painter->drawRect() ) and painting. And the worse is seen on screen. I dont know why but image isn't painted anymore (well ill fix it somehow) but this huge gap :| I dont know where is it from :|
I'm putting sources in attachment. Could you take a look at them?
Last edited by naresh; 15th March 2006 at 19:12.
I had to remove source code, here is new one (fixed a bit btw). Could you take a look at it?
Ok i have an idea how to calculate y position for all items... but only thing I need is to pass QTreeView::verticalOffset() (whitch is virtual protected)... best way I see is passing it with QStyleOptionViewItem (to QItemDelegate::paint(...)). Any idea how to do that?
Ok, I think I know what wrong. Looks like the view doesn't ask for the sizeHint each time it is resized, but only when an item changes. So you have to force the model to emit layoutChanged() on every view resize. Unfortunately you can't do it in the delegate, you should probably install an event filter on the view and provide a wrapper method for the model, which will emit layoutChanged() whenever the width of the view changes. It should be enough for your items to resize properly.
I checked throu whole painting (from view to delegate) and it paints wrong becouse of QTreeView:aintEvent(...). It calculates y position of each item depending on verticalStepsPerItem(). It can't be never calculated well becouse items have different heights... I've reimplemented this method but atm i have one problem... verticalScrollBar can move slider under all items. So now i don't know how to control verticalScrollBar. I've tried hiding it when viewport height is >= sizeHint height but there always left gray space for scroll bar. here is some code. Changing maximum for scrollBar causes scrollbar appear and disappear and so on... Also this painting seems to be really slow... Any ideas how to make it faster and control scrollbar? I'm placing source code to see what i have already done (and what am i missing)
EDIT: Another strange thing... It seems that items with descr have bigger range, i mean tooltip for those items is shown even if i place cursor on item under it :|
Last edited by naresh; 17th March 2006 at 18:22.
At least, after about a week of work it works like it should. First of all i've read whole thread few times to get it how was it painted before. It was taking width stright from viewport not from QStyleOptionViewItem. So i've added int width into delegate and method setWidth(int). I've reimplemented only resizeEvent for my rosterWidget class calling QTreeView::resizeEvent after setting width for delegate, and emiting layoutChanged for model (by my method emitLayoutChanged()). Delegate calculates sizeHint depending on width. I also added after every collaspe/expand item setting width (becouse scrollbar may appear and the viewport may resize without resizing widget). Now it paints and update well... As i expected. Thank you wysota for all your precious tips. Without them i would never make it.
Bookmarks