QSqlTableModel::select() resets combobox populated with it



  • Hello, I'm trying to implement some simple behaviour with QSqlTableModel and QCombobox, but I faced a problem I cant find elegant solution for. Hovewer I believe it should be.

    My partucular case:

    I have one widget (window) with a table model (QSqlTableModel) and views and controls mapped with it to edit it. And I have another widget (window) where I have combobox bound with this model (using QCombobox::setModel() and QCombobox::setModelColumn()).

    Looks like a trivial common task. But then I try to do the following:

    Open combobox and choose some item.

    Open another window and change the model.

    As soon as I submit changes, combobox is reset (not good!)

    If QSqlTableModel::SubmitPolicy is OnManualSubmit everything works fine but only because all changes are cached. As I call submitAll() to save changes to database, combo will also be reset. But some time I'll have to call submitAll().
    If QSqlTableModel::SubmitPolicy is OnRowChange i'll be forced to call submitAll in case I delete rows and again combobox value would be lost.

    bq. For OnFieldChange and OnRowChange, only one row may be deleted at a time and only if no other row has a cached change. Deletions are submitted immediately to the database. The model retains a blank row for successfully deleted row until refreshed with select().

    Are there any ways avoid combobox resetting during the repopulation of the model bound with it? Or this scheme itself is not right? Every opinion would be useful.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Do you change the content of the current item ?



  • It doesn`t matter. My problem is that when I use QCombobox::setModel() and have to (or want to) reselect the model combobox current item index is always reset to -1. Is some simple way (without much coding) to preserve selection in combobox through select() process?

    In some frameworks (e.g. Delphi) if there is a control (widget) bound with some dataset, you can detach that kind of controls while doing some manipulations with the dataset.


  • Lifetime Qt Champion

    What you could try is connect a slot to the modelAboutToBeReset signal where you save the current index and one to the modelReset signal where you restore the index.

    Hope it helps



  • I thought of something like this, but hoped that there is a solution to avoid manual data manipulation, cause it may affect fine tuned mechanism designed by developers of Qt library.

    Any way, thank you for suggesting the idea to use modelAboutToBeReset and modelReset signals. It works fine in my case.


  • Lifetime Qt Champion

    You may very well have found an unusual behavior.

    You should bring the question on the interest mailing. You'll find Qt's developers/maintainers there (this forum is more user oriented)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.