[Qt-interest] Get mapped value from a QSqlRelationalTableModel
Linos
info at linos.es
Mon May 16 19:44:21 CEST 2011
Ok,
sorry for the confusion, i don't use usually this classes directly because i
have written any subclasses to deal with database work because the classes in QT
are too limited, and in my model works the way i have sent you, the way to do
what you want in vanilla QT (i think) it is:
-------------------------- cut ----------------------------
QSqlRelationalTableModel *sourcerel = QSqlRelationalTableModel(this);
sourcerel->setTable("source");
sourcerel->setEditStrategy(QSqlTableModel::OnManualSubmit);
sourcerel->setRelation(2,QSqlRelation ("maptable","mapindex","mapvalue"));
sourcerel->select();
QSqlRelation *relation = sourcerel->relation(2);
QSqlTableModel *relModel = sourcerel->relationModel(2);
relModel->index(0, relModel->fieldIndex(relation->indexColumn())->data()
<---- integer value
relModel->index(0, relModel->fieldIndex(relation->displayColumn())->data()
<---- string value
-------------------------- cut ----------------------------
I have not tested the code and i don't write usually c++ code (i use PyQt), so
could be any syntax error but you should get the idea.
Regards,
Miguel Angel.
El 16/05/11 18:28, Petric Frank escribió:
> Hello,
>
> not working - tried that already. You are right when it comes to the method
> .setData - here you have to pass the original value not the mapped one in case
> of a mapped column.
>
> It still returns the mapped value from table "maptable" column "mapvalue" (in
> my case a string).
>
> What i want to get is the value of table "source" column #2 (which is in my
> case a number).
>
> A more detailed example:
> -------------------------- cut ----------------------------
> QSqlRelationalTableModel *sourcerel = QSqlRelationalTableModel(this);
> sourcerel->setTable("source");
> sourcerel->setEditStrategy(QSqlTableModel::OnManualSubmit);
> sourcerel->setRelation(2,QSqlRelation ("maptable","mapindex","mapvalue"));
> sourcerel->select();
> QVariant var1 (sourcerel->data (sourcerel->index(0, 2), Qt::EditRole));
> QVariant var2 (sourcerel->data (sourcerel->index(0, 2), Qt::DisplayRole));
> -------------------------- cut ----------------------------
>
> Both variables - var1 and var2 - contain the same (mapped) value. var1 should
> contain a number, not a string.
>
> Any more hints ?
>
> regards
> Petric
>
> Am Montag, 16. Mai 2011, 12:04:51 schrieben Sie:
>> Hi,
>> use Qt::EditRole in .data method.
>>
>> Regards,
>> Miguel Angel.
>>
>> El 16/05/11 03:46, Petric Frank escribió:
>>> Hello,
>>>
>>> i use a QSqlRelationalTableModel instance. It ha a mapped field into an
>>> into an other table. I used a QSqlRelation to do that.
>>>
>>> This is ok for most cases, but sometimes i want to obtain the mapped
>>> value instead of the replaced one.
>>>
>>> How to get the original value ?
>>>
>>> Little code snippet:
>>>
>>> ---------------------- cut ---------------------
>>> QSqlRelationalModel relmodel (...);
>>> relmodel.setTable ("source");
>>> relmodel.setRelation (2, QSqlRelation ("maptable", "mapindex",
>>> "mapvalue"); ...
>>> QVariant value (relmodel.data (relmodel.index (7, 2));
>>> ---------------------- cut ---------------------
>>>
>>>
>>> Using the last statement i get the value mapped to the one of table
>>> "maptable" taken from column "mapvalue".
>>> What want is to get the original value from table "source" column number
>>> 2.
>>>
>>> Of course i want to keep the mapping, because i need it also.
>>>
>>> Any hints ?
>>>
>>> regards
>>>
>>> Petric
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at qt.nokia.com
>>> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list