[Interest] Most direct path to using a QComboBox to edit a cell in a QTableView?

K. Frank kfrank29.c at gmail.com
Tue Jun 26 23:11:24 CEST 2012

Hello Tony!

Thanks again.  I do have some follow-up questions.

On Tue, Jun 26, 2012 at 12:28 PM, Tony Rietwyk <tony at rightsoft.com.au> wrote:
> Hi Frank,
> I haven't used QItemEditorFactory.  My reading of the docs suggests this
> only applies if all of your strings are to be edited via the same combo.
> None of the interfaces include a QModelIndex, so logically it can't be used
> to provide different editors for different columns.

Yes, I was kind of worried about that.  In my case my views and models
are associated with one another one to one.  (That is, I don't have more
than one view connected to the same model.)  All of the fields in a given
column of a view will use the same list of items in the combo box, but
different columns will have different lists of items.  So whether I do it
with an editor or a delegate, I need to be able to have the editor and/or
delegate depend on the column.

> I think that means you will need to provide a QStyledItemDelegate override.
> For a given view, you can use just one item delegate for all non-standard
> columns, or have separate delegates for different columns (or rows).

In this scheme I would have different (instances of the) delegates for
different columns.  Makes sense.

> I
> suggest that you load up the strings from the database only once, and store
> them in the delegate.  Then you override createEditor to create the combo
> box, and add the pre-loaded items.

This part I think I understand.

> Note that delegates don't have to be specific to a particular view.

Here's where I don't understand how things work.  Is a delegate
associated with a view or a model?  I would think with the view
because delegates seem to do "view" kinds of things -- display
and UI interaction, rather than "model" things -- data.

In my specific case of a QTableView, is a delegate associated
with the view, with a cell in the view, or with a row or column of
the view?

Is the delegate that is associated with the view (or some part
thereof) a specific instance of a delegate class, or is it a
class (i.e., not an instance) that the machinery under the
hood somehow instantiates automatically, as needed?

And how, specifically, do I associate a delegate with a view
(or a specific part of a view)?

Let's say I derive my custom combo-box delegate from
QStyledDelegate (along the lines of your TSpinBoxDelegate
example, below).  Then I instantiate my custom delegate
and load up that specific instance with the desired items
for its combo box.  How to I attach that instance of that
delegate to a specific column of one of my QTableViews?

That's the part I'm fuzzy on.

> Here is
> one that I use whenever I have a column of ints with a range:
> class TSpinBoxDelegate : public QStyledItemDelegate
> {
>        Q_OBJECT
> private:
>        int             minValue;
>        int             maxValue;
> public:
>        TSpinBoxDelegate(int min,  int max,  QObject *parent = 0);
>        QWidget *createEditor(
>                QWidget *parent,
>                const QStyleOptionViewItem &option,
>                const QModelIndex &index) const;
> };
> ................
> TSpinBoxDelegate::TSpinBoxDelegate(int min,  int max,  QObject *parent) :
> QStyledItemDelegate(parent)
> {
>        minValue = min;
>        maxValue = max;
> }
> QWidget *TSpinBoxDelegate::createEditor(
>                QWidget *parent,
>                const QStyleOptionViewItem &option,
>                const QModelIndex &index) const
> {
>        Q_UNUSED( option );
>        Q_UNUSED( index );
>        QSpinBox *editor = new QSpinBox(parent);
>        editor->setMinimum( minValue );
>        editor->setMaximum( maxValue );
>        return editor;
> }

This makes sense.  I think I'm clear on how to implement a custom
delegate -- I just don't know what to do with it after I have it.

> Hope that helps,

Yes, thank you.

> Tony.


K. Frank

More information about the Interest mailing list