PDA

View Full Version : Mapping.. ComboCheckBox using QDataWidgetMapper to SQLite



stanislavf
23rd September 2016, 21:20
Application created in Qt Creator uses combo check boxes. ComboCheckBox has uncheckable title item and checkable items:

-- Choose position

[ ] option1

[ ] option2

...

It is constructed by the following approach:


// Model construction.
QStandardItem* title = new QStandardItem(injuries.at(0));
model.insertRow(0, title);

for (int i = 1; i < injuries.length(); i++)
{
QStandardItem* item = new QStandardItem(injuries.at(i));

item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
item->setData(Qt::Unchecked, Qt::CheckStateRole);

model.insertRow(i, item);
}

// Setting model for combo chech box.
comboCheckBox->setModel(&model);

I'm trying to map data from SQLite to this combo check box by this way:


mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->addMapping(comboCheckBox, model->fieldIndex("inj1"), "currentIndex");

But to no avail. The same mapper works fine with usual check boxes:


mapper->addMapping(checkBox, model->fieldIndex("inj1"), "currentIndex");

Is any way to map checkable flags from so kind of combo check box to the row of smth of the SQLite data base?

I have failed to map even the current index not to mention separate checkable fields. Your elegant solutions would be just by the way for not very fancy combo check box.

wysota
24th September 2016, 10:33
You need a custom delegate that will convert model data to editor data (QAbstractItemDelegate::setEditorData) according to your needs. Of course you'll also need a mapping the other way round (QAbstractItemDelegate::setModelData).

anda_skoa
24th September 2016, 10:40
What is a combo check box?

Cheers,
_

stanislavf
28th September 2016, 21:06
Thank you, wysota.

But why do the following delegate doesn't work just for changing index:

mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
mapper->addMapping(comboCheckBox, model->fieldIndex("inj1"), "currentIndex");
What is the use of manual delegate?
My editor (a la combo check box has the model already). To which another model I should delegate it?


What is a combo check box?
It is a combo box with checkable items expect the title (first item).

anda_skoa
29th September 2016, 10:26
It is a combo box with checkable items expect the title (first item).
Since that appears to be a custom class, does it have a writable "currentIndex" property?

Cheers,
_