⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QComboBox map with QSqlTableModel column

  • I am mapping a QSqlTableModel to a form-like output that has a few QComboBoxes I just added. This program has to be done today so if anyone can help me ASAP I would really appreciate it.
    To map to a column I understand I need to so something like this
    mapper->addMapping(comboBox, col, "currentIndex");

    but there are multiple records with the same values and when I used comboBox->setDuplicatesEnabled(false); it didn't take out the duplicates.
    And when I add a new record I need the comboBox's value to be empty but when I set the current index to -1 it makes it empty but it also adds a "-1" as an item to the comboBox list which obviously isn't good.

    Also, I have a field for State and I would like all the states to be an option not just the ones already in the tableModel so is there a way to do this? I thought about using stateCombo->clear() then stateCombo->addItems() to add all of them but the documentations for clear() says the comboBox's model will also be cleared when this is called so I'm pretty sure it won't be mapped to the tableModel and therefore not saved to it.

    Thanks for any help you can give me!

  • Also, when I go to a record that doesn't have anything database in a field that is a comboBox values of a number such a "1", "3", and "0" are being added to the comoBox lists and being made the value of that field for that record. This makes absolutely no sense to me...

  • I've figured out some of this. I started just looping through my QSqltablemodel and adding all the values of the column to a QStringList then removing duplicates and then making a QStringListModel and setting that as the model instead of my actual TableModel. But in my table model it saves the index of the value selected in the combo box because of the "currentIndex" part in the mapper line. But I don't want that is there a way to have it save the actual text instead? And then I'll just use
    comboBox->setcurrentIndex(comboBox->findText(tableModel->index(row, column).data().toString()));

  • So, if I understand you correctly, you have a table in your database that looks like this:
    //ID Name Foo Bar
    // 1 Do ...
    // 2 Ray
    // 3 Mi
    // 4 Fa
    // 5 Fa
    // 6 Do
    // 7 So
    // 8 Mi
    And you want to put the values under Name in a combo box. However, those values need to be unique. At the same time, these values correspond to an ID value. There may also be other columns that you wish to ignore.

    The question then is: which of the ID values do you want to associate to your names? First occurrence? Last occurrence? Something else?

    BTW: as I mentioned "elsewhere":/forums/viewthread/24354/#112421 as well: QAbstractItemModel is IMO not great for manual use. Use it as an interface between models, proxy models and views.

  • Yes I do have an ID value that is unique. It isn't exactly like what you have it that matters it is more like
    to indicate the service ID and location it is at, so will always have 5 numbers (for now at least) but could start with 2 letters. But the values in fields for the combo box are not unique. If you mean need to be unique as in their output only showing each once then yes thats exactly what I need.

    It doesn't matter to me which ID value occurrence is associated with with the names. I would think the first occurrence would be easiest and slightly more efficient since it might not have to go through the whole table.

    But if there is something better to use for this I'm open to try whatever.

  • Then, the trick is just to use the correct query, and use a [[doc:QSqlQueryModel]] as your model for the combo box.

    A query like
    FROM tableName
    should do the trick.

  • That mapped them to the combo boxes perfectly but is still saving the items as index numbers. Is there a way around that? Because after a while if each record is edited and resaved there will just be indexes and no actual text saved in those columns in the table.

  • You have not told us everything we need to know then. So far, we've been discussing how the values in the combo box list can be set from a data base. But now you are talking about also storing the result in some format in the database. However, you have not shown us how your combo box value is connected to the data base.

  • In my original post I kind of did. But I guess that's not near enough information.

    I set my mapper up like this (and tableModel is a QSqlTableModel with a manual submit edit strategy)
    mapper = new QDataWidgetMapper(this);
    mapper->setItemDelegate(new QSqlRelationalDelegate(this));

    then I populate and map the combo box
    QSqlQueryModel *qModel = new QSqlQueryModel;
    qModel->setQuery("SELECT DISTINCT Name FROM First_List");
    mapper->addMapping(comboBox, 6);

    I originally had (comboBox, 6, "currentIndex") which I thought was the reason why it was putting the index but I guess that's also the default because it's doing the same thing without it there.
    Then when I save I call

    I thinks that's pretty much everything involved in connecting the combo box to the database

Log in to reply