[PyQt] QTableModel (custom) with QComboBox in a cell... (take 2)

Matt Newell newellm at blur.com
Wed Mar 19 16:36:22 GMT 2008


On Wednesday 19 March 2008 00:43:46 Dirk Wagener wrote:
> Hi
> I sent this before, but got no replies.
> Here we go again:
>
> I am implementing a custom model for a table view. I have an editable cell
> in which the user can enter a value. What I want to do is to make the cell
> a QComboBox so that the user can enter a text value for the cell OR select
> some value from a drop-down list. I am not using delegates currently.
> What is the correct (easiest) way to achieve this?
>

You either need to write a custom delegate class or a QItemEditorFactory 
class.  I've never used the QItemEditorFactor, i've just used custom 
delegates.

You'll need to implement createEditor, setModelData, and setEditorData.  You 
can pass your data from the model as a qstringlist, or any custom type 
wrapped as a QVariant(You can even use your own roles if needed). For example 
you could pass a QStringList for the editor role and QString for the Display 
role to use as the current value, which would give you enough info to fill in 
a combo box.

Make sure to call setItemDelegate on the view, passing your custom delegate 
class.

Matt


More information about the PyQt mailing list