4

I have QListView with custom implementation of QItemDelegate. MyItemDelegate reimplements createEditor() to show custom widget. Size of widget is dependant on content. By default, each row height is about 20px (one row), but my editor has bigger height. I was trying to override QItemDelegate::sizeHint() method, but is doesn't contains reference to editor, so I couldn't calculate correct size.

How can I make QListView resize rows to actual size of editor?

developer
  • 319
  • 2
  • 13

1 Answers1

0

You should emit layoutChanged after creating an editor, if you could not override sizeHint correctly. But it should be enough to override sizeHint.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • How it could be enough to use sizeHint()? All I have inside sizeHint() is (QOptionViewItem)option and (QModelIndex)index paremeters. This information alone is not enough. I need (QWidget)editor itself. Acutally, all i need is to return editor->size(). – developer Feb 05 '14 at 13:02
  • Just save pointer (that you return from createEditor) of current active editor as a member of delegate and use it in sizeHint. It is useful to save it via QPointer template, so you can track editor existance by checking pointer with null. – Dmitry Sazonov Feb 05 '14 at 13:45
  • Saving pointer as member of delegate looks doubtful. It could happen that sizeHint() will work with other instance of editor (i use persistent editors) – developer Feb 05 '14 at 13:58
  • You may keep a map of opened editors. 'QMap< QPersistentModelIndex, QPointer< QWidget > >'. I don't see other ways. You need some logic to make relation between index and editor... Anyway, try to emit layoutChanged after creating of an editor. And set correct minimum size to an editor. – Dmitry Sazonov Feb 05 '14 at 14:05
  • Well, actually there're other ways, but all of them look a bit ugly. My original goal was to display list of widgets. Maybe you know some other, easier way to achieve it? – developer Feb 05 '14 at 21:00
  • Yes! Scroll view + layout. It is bad practice: trying to use item views to display list of widgets. But a lot of developers do it. But if you have a lot of widgets (more than 100-200) and you need good interaction - it is a point to use QGraphicsScene. – Dmitry Sazonov Feb 06 '14 at 07:12
  • In WPF/C# I could create list of complex interactive widgets with thousands of items, and list would reuse them as you scroll. I thick "ScrollView + layout" will lack with feature. Suppose, I want to create chat with hyperlinks, buttons and progress bars in it. What approach should I use? ScrollView will have bad performance. Delegate can display only static data. Otherwise you should switch it to persistent edit mode, which has its own problems. QGraphicsScene should have the same problem as scrollview. – developer Feb 06 '14 at 12:45
  • QGraphicsScene is big overhead for chat. It is better to specify EXACT question next time. For chat you may use QTextBrowser. It require some tricks to insert animated smiles, but it is another question. Hyperlinks and text formatting is supported by default. – Dmitry Sazonov Feb 06 '14 at 13:12
  • Well, actually i asked exactly what i wanted to ask. QTextBrowser is very bad choice. Poor css with almost no way to add interactivity. – developer Feb 06 '14 at 13:39
  • You wrong. QTextBrowser supports Rich Text formatting. It can much more that QSS. And what kind of interactivity you need? – Dmitry Sazonov Feb 06 '14 at 14:03
  • P.S. you may check chat here: http://www.iccup.com/files.html (Dota2 tab -> Launch). But anyway, if you want a lot of problems instead of using expected classes - it's your choise. – Dmitry Sazonov Feb 06 '14 at 14:05