Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Model value persist in QCombobox after loosing focus
Forum Updated to NodeBB v4.3 + New Features

Model value persist in QCombobox after loosing focus

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jaosimt
    wrote on last edited by
    #1

    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);
    model->setTable("stock");
    model->setFilter("stockid='"+keyS+"'");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);

            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"));
    
            model->select();
    
            unitModel = model->relationModel(unitId);
            ui->cbUnit->setModel(unitModel);
            ui->cbUnit->setModelColumn(unitModel->fieldIndex("description"));
    
            suppModel = model->relationModel(suppId);
            ui->cbSupplier->setModel(suppModel);
            ui->cbSupplier->setModelColumn(suppModel->fieldIndex("description"));
    
            pCatModel = model->relationModel(pCatId);
            ui->cbPCategory->setModel(pCatModel);
            ui->cbPCategory->setModelColumn(pCatModel->fieldIndex("description"));
    
            iCatModel = model->relationModel(iCatId);
            ui->cbICategory->setModel(iCatModel);
            ui->cbICategory->setModelColumn(iCatModel->fieldIndex("description"));
    
            QStringList yesNo;
            yesNo << tr("Yes") << tr("No");
            serialedModel = new QStringListModel(yesNo, this);
            ui->cbSerialed->setModel(serialedModel);
    
            mapper = new QDataWidgetMapper(this);
            mapper->setModel(model);
    
            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"));
    
            mapper->toFirst();@
    

    [edit : fixed typo in title, Eddy]

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jaosimt
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jaosimt
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            [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.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jaosimt
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved