Have you tried QAbstractItemModel::data with role=Qt::SizeHintRole?
Have you tried QAbstractItemModel::data with role=Qt::SizeHintRole?
Not yet... But what shud I return from there ? I dont have font info in data()
Also how do I do it with QTreeWidget, where I am not using model ? I tried rowHeight and indexRowSizeHint, I put a breakpoint, but the breakpoint was never reached![]()
Yes, the fact that stuff like FontRole, SizeHintRole, ForegroundRole etc is 'part' of the model is a bit strange now and then.
Perhaps QAbstractItemDelegate::sizeHint will help you, then.
You're rendering in the delegate, so there you should be able to access font info etc.
And make sure not to set uniformRowHeights on the TreeView (if you want rows of different heights).
Ya i had implemented sizeHint for delegate, but in vain.
Actually sizeHint is called once initially. So I can set height accordingly. But when I resize the window, treewidget also resize and on this resize I want the row height to increase.
Which function should I look for ?
I also tried visualRect, but it doesnt effect too. Also rowHeight and indexRowSizeHint are never called
.
Isnt there any function to update the row height on resize event ?
Last edited by aamer4yu; 24th June 2008 at 13:07.
The item size should not be dependent on the view size. Make sure uniformRowHeight property of the view is set to false.
It is false. I am getting different row heights.
I was asking how to set row heights upon resize. Say if window is reduced in width, columnwidth is also reduced. So now I want to resize row height to fit the content acc to new height.
I am able to set the row height initially once by QItemDelegate::sizeHint() function. I was having 2 errors in it previously -
I was calculating row height based on -
Problem was with Qt::ElideNone. I was getting the original text and the line count was always 1Qt Code:
do { line++; elided = fontMetrics.elidedText(textToRender,Qt::ElideNone,width-2); charsRendered += elided.length(); textToRender = text.mid(charsRendered); }while(!textToRender.isEmpty()); height = height*line;To copy to clipboard, switch view to plain text modeIs elidedText() supposed to return the original text if mode is Qt::ElideNone ??.
Second problem was with width = optio.rect.width(); inside sizeHint() function.
I found that in QTreeView::indexRowSizeHint() function, width is set to -1 for some optimization. Hence I was not getting the proper column width. I set the width manually and now I get the line numbers correctly.
So far so good. I can assume my application wont be resized and am taking some default column width.
Still if someone know how to set row height on resize, let me know.
Thanks for ur help Caduel and Wysota![]()
I landed in another problem.
The QItemdelegate::sizeHint() doesnt work same with QTreeWidget and QTreeView.
In the Simple DOM example, in the QItemdelegate::sizeHint(), I am getting proper font in option parameter. While in QItemdelegate::sizeHint(), delegate set for MyTreeWidget, I dont get the proper font.
Hence while calculatin row height, I get a difference in these 2 implementations. Why is it so ??
Also another doubt, if we setWordWrap(true) on QTreeView, isnt it supposed to display text in multiple lines ? One can easily check using Simple DOM model and setting word wrap to true for the treeview.
Last edited by aamer4yu; 25th June 2008 at 10:13.
There is a sizeHintChanged() signal in QAbstractItemDelegate. You can apply an event filter to the view so that you know when its size changes and emit that signal from within the delegate and the view should re-ask the delegate for new sizes then. I think it's worth to try...
Sure it does
What do you consider a "proper" font?In the Simple DOM example, in the QItemdelegate::sizeHint(), I am getting proper font in option parameter. While in QItemdelegate::sizeHint(), delegate set for MyTreeWidget, I dont get the proper font.![]()
Yes, but this works only on word-breaks, I think. I did see it work, at least.Also another doubt, if we setWordWrap(true) on QTreeView, isnt it supposed to display text in multiple lines ?
By proper font I mean the font I set on the TreeWidget or the TreeView.What do you consider a "proper" font?
The QItemdelegate::sizeHint() doesnt work same with QTreeWidget and QTreeView.
Yes, it doesnt for me. Well I meant the overrided sizeHint. What I did was I overrided sizeHint of my delegate. Say MyDelegate::sizeHint. I am using this delegate in 2 programs.
One MyTreeWidget/MyTreeWidgetItem (P1), and other modified version of Simple DOM(P2) Model. In P1 I set MyTreeWidget->setFont(myFont) after creating the tree. In P2 I did the same.
Now in P1 , in MyDelegate::sizeHint, I get the font from option.font as system default - MS Shell Dialog2(something like that) . and in P2 , in MyDelegate::sizeHint, for option.font I get font as myFont. This is what I mean that sizeHint doesnt work same in both cases
As for Word wrap, did u try setting word wrap to true for QTreeView in simple DOM model example and shrink the window width ??
For sizeHintChanged() , I will surely try.
But its gonna fail. In MyDelegate::sizeHint() function, i get option.rect.width() as -1. This is set in QTreeView::indexRowSizeHint() function for some hack
You can verify from the QTreeView::indexRowSizeHint() function.
Bookmarks