How to create delegates that are always editable?
-
I'm working with Qt's model/view framework. It's a fairly normal model and view, where each row can be thought of as an object, and each column is an attribute on that object. For some of the columns, I want to display something more than text. For example, there is a "Color" column where I want to display a widget that shows the current color and allows you to click on it to open up a color-selection dialog box.
From what I understand, the best way to do this would be with a delegate. The delegate for the color column would load a custom editor widget. This custom editor widget would be a button (or some other type of clickable widget) that, when clicked on, would open up the color-selection dialog box.
However, it looks like delegates only load up their editor widget when the edit trigger is triggered. This means I have to click on the cell once to "edit" it and load up the editor widget. Then I have to click on this editor widget to open up the dialog box. What I'm looking for is a way to have this editor widget always loaded up so that I can click on it directly at any time.
My team has done this in the past using
QAbstractItemView::setIndexWidget()
, but I'm wondering if there is a more "proper" way to do this with delegates. -
Hi,
You don't need any intermediate widgets. When reimplementing createEditor, you can return a QColorDialog.
You have an example in Python here.
-
Hi,
You don't need any intermediate widgets. When reimplementing createEditor, you can return a QColorDialog.
You have an example in Python here.
@SGaist I think that approach may work for the color column, so thank you for explaining it.
We also have a "Transparency" column, which is a little more complicated than the color column. I probably should have started with it as my example. The widget in the transparency column actually has two buttons: a cube and a downward arrow. Clicking on the cube just toggles between opaque and transparent. Clicking on the arrow opens up a little slider for fine control over the level of transparency. In addition, we'd like the buttons to respond when the user hovers over them, something to indicate that they are clickable.
This is where I'm getting the idea of a delegate that is always editable, so that the widget is always loaded. We really want it to be apparent to the user that this is a button that they can interact with. Would using
QAbstractItemView::setIndexWidget()
be the standard way to do that? Or should this still be done somehow with delegates? -
setIndexWidget should be used only for showing some static data in some cell. Not to fill views with widgets.
You can still use a delegate for that transparency editor. It will need a bit more work though.
The editorEvent method will help you with that. -
setIndexWidget should be used only for showing some static data in some cell. Not to fill views with widgets.
You can still use a delegate for that transparency editor. It will need a bit more work though.
The editorEvent method will help you with that.@SGaist Thanks for your answer. I'll look into
editorEvent()
.I found another answer online that mentions
QAbstractItemView.openPersistentEditor()
. Beyond performance considerations, would there be a strong reason not to use this method? (The lists I'm working with would have, at most, hundreds of items. Usually it's less than a hundred, so performance isn't the strongest consideration.) -
It still means that you will have a hundred of widgets at least depending on how you implement your editor opened all the time. That still burns resources for not much benefit.