QItemDelegate sizeHint() with QComboBox
-
I have a custom QItemDelegate class which uses a QComboBox as an editor. QComoboBox provides a method
sizeHint()
which returns the size hint to display all the items in the list so the layout won't change when showing the dropdown. Therefore, I would like to make myQItemDelegate::sizeHint()
to return theQComboBox::sizeHint()
.
However, outside ofQItemDelegate::setEditorData()
andQItemDelegate::setModelData()
I cannot access the QComboBox as I don't get theQWidget* editor
parameter.How can I fix this? I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column. Any hints? Is there no way to access the editor inside of
QComboBox::sizeHint()
? -
You basically answered yourself.
Normally editors are created and deleted of the fly. If you want to use editor's sizeHint() you should make it persistent and created before QItemDelegate::sizeHint() is called.I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column.
And how this makes it bad ?
-
@alex_malyu So I am supposed to make the editor become a private member of my custom item delegate class?
I assume(d) that there is a pretty good reason why I get a widget pointer for the editor in thesetEditorData()
andsetModelData()
methods.I though that making the combobox editor become a private member of the item delegate is a bad idea because then each row of the column would use the same combobox. As I understood the purpose of
createEditor()
is that there is one editor per row when I set the delegate for an entire column in my tableview.When I make the editor become a private member, will it still just show up when the user double clicked the item and vanish automatically after the edit has been completed?
-
As far as I understand, if you need a persistent editor you need to subclass an item view.
Problem with delegate is - that you do not have any way to prevent editor to be deleted.
If you make editor private member of itemview subclass and to override 2 functions below it should do it.bool QAbstractItemView::edit ( const QModelIndex & index, EditTrigger trigger, QEvent * event ) [virtual protected]
void QAbstractItemView::closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint ) [virtual protected slot]You also might need to write additional resizing/positioning code so editor is drawn properly,
I believe I've done something like this, but it was long time ago and may be even done with Qt 3 , so I can't be sure I can provide enough details. -
@alex_malyu Thank you for your reply. I will give this a try in the following days.
-
@Joel-Bodenmann said:
How can I fix this? I thought of making the combobox become a private member of the item delegate class but that is not a good solution as I set the same item delegate for an entire column. Any hints? Is there no way to access the editor inside of QComboBox::sizeHint()?
Hi!
Hm... the easiest solution which came to me is just create ComboBox instance inside delegate sizeHint function, call combobox size hint function and return it : ). ComboBox will be destroyed automatically at the end of delegate sizeHint function.But frankly, i didn't get your idea about ComboBox sizeHint. For example I also have comboboxes as editors for my delegates, I didn't implement sizeHint at all, and it still work quite well.
-
@Harb When I understood correctly a column of a QTableView which has a delegate assigned will automatically resize itself to the
QItemDelegate::sizeHint()
size. Therefore, I could return the required width of the column to display the full text in the cell, right?My custom delegate uses a QComboBox. The combobox is populated with items of different length. Now, let's assume there is just one row in the table and the item in the cell with the custom delegate is the item in the combobox with the shortest width. Therefore, the column will be just wide enough to display that text. When the user then clicks on the cell and the combobox dropdown shows up the user will not be able to see entries in the combobox which are longer. Items in the combobox which are longer than the width of the column will be shorted and three dots will be inserted.
I took a screenshot so you can see what I mean: http://paste.ugfx.org/sores/7505d64a54e0/bcdde374e0fc.jpg
As you can see there is one item in the dropdown which cannot be displayed in full length because the column width is too small.What I would like to do is using the
QItemDelegate::sizeHint()
to return theQComboBox::sizeHint()
so the column will automatically be wide enough to display all items (because theQComboBox::sizeHint()
returns the size so all items can be displayed.