Important: Please read the Qt Code of Conduct -

Model value persist in QCombobox after loosing focus

  • Hi all,

    I know this is supposed to be simple but i cant make it to work accordingly.

    I have a 3 QComboBox mapped in a model column with selectable values from relationalmodel and 1 with selectable value from QStringList.

    As the form loads, all objects are populated perfectly, all QComboBox selectables can be selected but the QComboBox with selectables from QStringList is behaving oddly. For Instance, the current model value is "No" and i would change it to "Yes", the "No" value which is the current value from the model will persist after the QComboBox looses its focus.

    Her is my code...
    // Set up the table model.
    model = new QSqlRelationalTableModel(this);

            unitId = model->fieldIndex("unitid");
            suppId = model->fieldIndex("supplierid");
            pCatId = model->fieldIndex("prodcategory");
            iCatId = model->fieldIndex("invcategory");
            model->setRelation(unitId, QSqlRelation("units", "unitid", "description"));
            model->setRelation(suppId, QSqlRelation("supplier", "supplierid", "description"));
            model->setRelation(pCatId, QSqlRelation("pcategory", "categoryid", "description"));
            model->setRelation(iCatId, QSqlRelation("icategory", "categoryid", "description"));
            unitModel = model->relationModel(unitId);
            suppModel = model->relationModel(suppId);
            pCatModel = model->relationModel(pCatId);
            iCatModel = model->relationModel(iCatId);
            QStringList yesNo;
            yesNo << tr("Yes") << tr("No");
            serialedModel = new QStringListModel(yesNo, this);
            mapper = new QDataWidgetMapper(this);
            mapper->setItemDelegate(new QSqlRelationalDelegate(this));
            mapper->addMapping(ui->leStockId, model->fieldIndex("stockid"));
            mapper->addMapping(ui->leDescription, model->fieldIndex("description"));
            mapper->addMapping(ui->leQtyMin, model->fieldIndex("qtymin"));
            mapper->addMapping(ui->leQtyMax, model->fieldIndex("qtymax"));
            mapper->addMapping(ui->leUnitPrice, model->fieldIndex("uprice"));
            mapper->addMapping(ui->leSrp, model->fieldIndex("srp"));
            mapper->addMapping(ui->cbUnit, unitId);
            mapper->addMapping(ui->cbSupplier, suppId);
            mapper->addMapping(ui->cbPCategory, pCatId);
            mapper->addMapping(ui->cbICategory, iCatId);
            mapper->addMapping(ui->cbSerialed, model->fieldIndex("serialed"));

    [edit : fixed typo in title, Eddy]

  • by the way, The said QComboBox does not have any event... that is why i wonder why it reloads the mapped model value after it looses its focus.

  • Uhm...could it be that the data in the database for the column serialed is not a "yes/no" set of values? I don't understand why you are loading the column as a string list instead of loading database values. I will give a try using the normal sql model for such combo to see if it keeps the value. If this works, it means that your string model has wrong values that cannot be set.

  • Thats the part i did not understand really...

    the table has 11 columns in it and one is named "serialed" of type character varying(3), it has 6 rows during this test, all "serialed" contain "No" values. the mapper works well because the QComboBox is populated accordingly on form load and the QComboBox choices looks pretty good as well...

    but when i put the yes/no in a table... it does worked perfectly. Do you suggest that i rather put the "Yes/No" in a table? because it is kind of kinky or something.... I mean, it is only a "Yes/No", it wont change, it wont be updated...

  • [quote author="jao" date="1323476396"]
    but when i put the yes/no in a table... it does worked perfectly. Do you suggest that i rather put the "Yes/No" in a table? because it is kind of kinky or something.... I mean, it is only a "Yes/No", it wont change, it wont be updated... [/quote]

    I was wondering if, maybe, there is some difference between the "yes/no" string values and those stored on the database, and if there is a constraint in the database that, for instance, accepts only "No " instead of "No" or something alike. Otherwise I cannot see what is wrong with your code, sorry.

  • Well, I already tried to change the "No" to "No " in the QStringList. Problem is, the mapper wont work on that particular field.

    Thanks anyway, for now I did make a table containing only a "Yes" and a "No" it is an additional count to the number of tables but it does work perfectly. ;D

Log in to reply