Qt World Summit: Register Today!

fill combobox with query results, but also <select>

  • Hi,

    I would like to fill a combobox using a QSqlQueryModel, which works fine btw.
    But I noticed that the default index is the result of the query with the lowest index.
    I'd like to be able to not fill in anything so is it possible to fill a combobox with <select> and then add the model?

    Like this:

  • @hobbyProgrammer
    You cannot have a combo which is both bound to a model and has some extra row inserted into it --- it's one or the other.

    There are several possible approaches:

    • Make your SQL query return a UNION ALL which includes a literal <select> as an extra row. Not really nice, because it makes the <select> be an extra row from the query.

    • Copy the result set from the query into another model (you probably only need a list, perhaps a QStringList) where you insert the extra <select> as an item and use that as the combo's model. If you don't get a better example, this is the most obvious to code.

    • Interpose a QIdentityProxyModel between the query model and the combo, and add the extra row there. I suspect this is the way it's supposed to be done(?).