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. QSqlTableModel::select() resets combobox populated with it
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel::select() resets combobox populated with it

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.2k 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.
  • L Offline
    L Offline
    levolex
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Do you change the content of the current item ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        levolex
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            levolex
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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