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. Update QTableView after QSqlTableModel insert
Forum Updated to NodeBB v4.3 + New Features

Update QTableView after QSqlTableModel insert

Scheduled Pinned Locked Moved Solved General and Desktop
5 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.
  • T Offline
    T Offline
    Tikani
    wrote on last edited by VRonin
    #1

    I'm writing an application over SQLite3 database for home bookkeeping.
    I'm using Qt 5.9.2, MinGW 5.3.0 32bit and QWidgets.

    Here I'm appending record to the QSqlTableModel (forgive me please for that kind of "ass-driven" development):

    void RegistryTab::on_btnAdd_clicked()
    {
        AddEditDialog d(type, this);
        if(d.exec() == QDialog::Accepted) {
            QSqlField field("id",QVariant::String);
            field.setAutoValue(true);
            QSqlRecord t;
            t.append(field);
            QHash<QString,QString>::iterator i;
            for(i=d.data()->begin(); i != d.data()->end(); ++i) {
                QSqlField temp(i.key(), QVariant::String);
                t.append(temp);
                t.setValue(i.key(),i.value());
            }
            modelsContainer->addRegistryRecord(type,t);
        }
    }
    
    void ModelsContainer::addRegistryRecord(RegistryType type, const QSqlRecord &record)
    {
        tableModels->value((*registryMap)[type])->insertRecord(-1, record);
    }
    

    But after appending I have such a picture:
    https://ibb.co/d1F66v

    How can I refresh the QTableView to see model changes immediately?
    Model's EditStrategy is "OnFieldChange".

    Thanks in advance!

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

      Hi,

      You should check the return value of insertRecord. Also, are you sure that your QSqlRecordObject contains something ?

      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
      • T Offline
        T Offline
        Tikani
        wrote on last edited by
        #3

        After that code execution QTableView displays empty cells in a row with an exclamation mark on the left side.
        But, if I restart the whole application, the data successfully appears => the data is inserted into underlying physical SQL table in database file.
        Summing up, I cannot find any way to make QSqlTableModel data changes be immidiately displayed in a QTableView.

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

          AFAIR and from the SQL Presenting chapter, an exclamation mark means that the change was not submitted.

          I'd try to call submitAll after insert to see if it changes anything.

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

          T 1 Reply Last reply
          1
          • SGaistS SGaist

            AFAIR and from the SQL Presenting chapter, an exclamation mark means that the change was not submitted.

            I'd try to call submitAll after insert to see if it changes anything.

            T Offline
            T Offline
            Tikani
            wrote on last edited by
            #5

            @SGaist
            Thanks a lot, the problem is solved, submitAll() was the solution indeed. Marking topic as solved.

            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