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

K. Frank kfrank29.c at gmail.com
Thu Jun 28 06:24:57 CEST 2012


Hello Tony!

Thanks for your pointers on how to hook the delegates and views together.
At the moment it looks like things are working the way I want.

On Tue, Jun 26, 2012 at 10:06 PM, Tony Rietwyk <tony at rightsoft.com.au> wrote:
> Hi Frank,
> ...
>> And how, specifically, do I associate a delegate with a view (or a
> specific part
>> of a view)?
>
> Search the docs for 'delegate' in QtAssisstant!  You will find the 3
> flavours of setItemDelegate.

Yes indeed.  In my case I want:

   void QAbstractItemView::setItemDelegateForColumn ( int column,
QAbstractItemDelegate * delegate )

(inherited by QTableView).

> The view holds reference(s) to the delegate(s), and uses it to paint and
> edit the associated cells.  Simplest way in your case, is to create a
> separate instance of the delegate class for each column that needs it.

Yes, that's what I'm doing, and it all makes good sense.

I implemented a custom delegate derived from QStyledItemDelegate
(along the lines of the TSpinBoxDelegate example you posted earlier)
that overrides createEditor to return a (pointer to a) QComboBox.  I
populate the QComboBox by connecting it (setModel) to a
QSqlTableModel.

The one additional thing I had to do was override setModelData.  This
is because the default behavior of setModelData is to set the model
data to the currentIndex property of the QComboBox, and I want to
set the model data to the QCombBox's currentText.  One additional
step, but all in all, not so bad.

(I do agree that it doesn't seem like the approach of using
QItemEditorFactor / QItemEditorCreatorBase could be made to work
because, as you noted, it doesn't seem like that approach gives you
the hooks to associate a custom editor with specific columns or rows
or cells of the QTableView.)

> Tony.

Thanks again for your help.  I appreciate it.

Things look good for the moment.


Best.


K. Frank



More information about the Interest mailing list