[Qt-interest] Subclassing QComboBox does not work with QSqlRelationalDelegate

Lynoure Braakman lynoure at gmail.com
Thu Apr 29 13:05:31 CEST 2010


Hello,

I'm making a widget DateComboBox, which is based on QComboBox.
It allows the user to pick dates from a dropdown list and one of the
options opens a calendar for manual selection of a date. I'm using
the widget through QDataWidgetMapper in order to edit the data from
a SQL database with minimum hassle. I have set a USER property to
tell what functions are used to read and write to this widget.
DateComboBox uses the write function to create an index for the
selected date.

The USER property however did not seem to work when it came to
writing. To my surprise QSqlRelationalDelegate had its own idea
about how to write to any widget that is a subclass of QComboBox:

>From qsqlrelationaldelegate.h:
 void setEditorData(QWidget *editor, const QModelIndex &index) const
{
    const QSqlRelationalTableModel *sqlModel = qobject_cast<const
QSqlRelationalTableModel *>(index.model());
    QComboBox *combo = qobject_cast<QComboBox *>(editor);
    if (!sqlModel || !combo) {
        QItemDelegate::setEditorData(editor, index);
        return;
    }
    combo->setCurrentIndex(combo->findText(sqlModel->data(index).toString()));
}

It uses setCurrentIndex directly instead of using the USER property,
which makes subclassing QComboBox a pain.

What the reason for this instead of having a USER property in QComboBox?
QItemDelegate::setEditorData also has an exception for QComboBox
but at least checks for the existence of the USER property, which feels
like a much better approach than what QSqlRelationalDelegate has.

Is there some more elegant way to get the USER property write function of
my own widget into use, other than subclassing QSqlRelationalDelegate
and overriding that setEditorData? I would like to fix it in such a way
that others can use DateComboBox without running into the same surprise.

-- 
Lynoure Braakman
lynoure at gmail.com



More information about the Qt-interest-old mailing list