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. Updating QStandardItemModel when records added to QSQLITE database
Forum Updated to NodeBB v4.3 + New Features

Updating QStandardItemModel when records added to QSQLITE database

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 4 Posters 5.1k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have a QStandardItemModel displaying records from a db through tableView. How can I update the the records displayed in tableView when a record is added to the db?
    Thank you.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      SQLite has no signaling capabilities (QSqlDriver::hasFeature(QSqlDriver::EventNotifications) == false) so you have 3 alternative routes:

      • if the database is updated just from your app just emit a signal every time you update it and have a slot refresh the model
      • if the database is updated just from your app but you can have multiple users/instances running at the same time use QTcpSocket to signal others when something changed
      • if the database can be changed externally the only choice is to run select from a periodic timer and check the returned table against yours

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G 1 Reply Last reply
      1
      • p3c0P Offline
        p3c0P Offline
        p3c0
        Moderators
        wrote on last edited by
        #3

        @gabor53
        You will also need to update it in the model. There are several methods provided:
        http://doc.qt.io/qt-5/qstandarditemmodel.html#setItem
        http://doc.qt.io/qt-5/qstandarditemmodel.html#insertRow-1
        http://doc.qt.io/qt-5/qstandarditemmodel.html#appendRow-1

        157

        1 Reply Last reply
        0
        • VRoninV VRonin

          SQLite has no signaling capabilities (QSqlDriver::hasFeature(QSqlDriver::EventNotifications) == false) so you have 3 alternative routes:

          • if the database is updated just from your app just emit a signal every time you update it and have a slot refresh the model
          • if the database is updated just from your app but you can have multiple users/instances running at the same time use QTcpSocket to signal others when something changed
          • if the database can be changed externally the only choice is to run select from a periodic timer and check the returned table against yours
          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #4

          @VRonin
          It is updated only from my app. Do I have to write my own signals and slots?

          VRoninV 1 Reply Last reply
          0
          • G gabor53

            @VRonin
            It is updated only from my app. Do I have to write my own signals and slots?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @gabor53

            • declare a signal in the class that does the update (I presume it's a QObject) and emit it after you perform the update
            • define a slot that takes care of refreshing the table
            • connect the two

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

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

              Hi,

              Out of curiosity, why not use QSqlTableModel since you are already using QTableView ?

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

              G 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                Out of curiosity, why not use QSqlTableModel since you are already using QTableView ?

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @SGaist
                I should have

                1 Reply Last reply
                0
                • VRoninV VRonin

                  @gabor53

                  • declare a signal in the class that does the update (I presume it's a QObject) and emit it after you perform the update
                  • define a slot that takes care of refreshing the table
                  • connect the two
                  G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

                  @VRonin
                  is there a tutorial somewhere about custom signals and slots? I wasn't able to find anything recent. Thank you.

                  VRoninV 1 Reply Last reply
                  0
                  • G gabor53

                    @VRonin
                    is there a tutorial somewhere about custom signals and slots? I wasn't able to find anything recent. Thank you.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @gabor53 That's pretty much the core of Qt: http://doc.qt.io/qt-5/signalsandslots.html

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gabor53
                      wrote on last edited by
                      #10

                      I came up with the following:
                      .h file:

                      
                      signals:
                          void RecAdded();
                      
                      private slots:
                          void updateTable();
                      
                      

                      In cpp:

                       connect(on_pushButton_Add_to_DB_clicked(),SIGNAL(recAdded()),this,SLOT(updateTable ()));
                      

                      The full cpp just in case: cpp

                      When I try to build it, it gives me the following error message:

                      C:\Programming\Projects\Folkfriends_1_0\review.cpp:125: error: invalid use of void expression
                      connect(on_pushButton_Add_to_DB_clicked(),SIGNAL(recAdded()),this,SLOT(updateTable ()));
                      ^
                      What causes this error? Thank you.

                      1 Reply Last reply
                      0
                      • p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by
                        #11

                        @gabor53 The reason is connect expects an object as its first parameter but your on_pushButton_Add_to_DB_clicked() returns a void and thus the error invalid use of void expression
                        Return an object from that function or use an object directly(makes it more easier to understand).

                        157

                        G 1 Reply Last reply
                        0
                        • p3c0P p3c0

                          @gabor53 The reason is connect expects an object as its first parameter but your on_pushButton_Add_to_DB_clicked() returns a void and thus the error invalid use of void expression
                          Return an object from that function or use an object directly(makes it more easier to understand).

                          G Offline
                          G Offline
                          gabor53
                          wrote on last edited by
                          #12

                          @p3c0
                          I understand now how it works. In this case the user clicks a button and the record is added to the database. What kind of object do you recommend to create to update the model?

                          1 Reply Last reply
                          0
                          • VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            You don't have to create any more objects, just replace on_pushButton_Add_to_DB_clicked() with the actual button something like ui->Add_to_DB

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            G 1 Reply Last reply
                            1
                            • VRoninV VRonin

                              You don't have to create any more objects, just replace on_pushButton_Add_to_DB_clicked() with the actual button something like ui->Add_to_DB

                              G Offline
                              G Offline
                              gabor53
                              wrote on last edited by
                              #14

                              @VRonin
                              Thank you. Now I have

                              connect(ui->pushButton_Add_to_DB,SIGNAL(recAdded()),this,SLOT(updateTable ()));
                              

                              which works. My new error message is

                              QObject::connect: No such signal QPushButton::recAdded() in ..\Folkfriends_1_0\review.cpp:125
                              QObject::connect:  (sender name:   'pushButton_Add_to_DB')
                              QObject::connect:  (receiver name: 'Review')
                              

                              In .h I have the following:

                              signals:
                                  void RecAdded();
                              

                              Is this correct?
                              Thank you.

                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                sorry my bad, it should be:

                                connect(this,SIGNAL(recAdded()),this,SLOT(updateTable ()));

                                then call emit recAdded() after you added the record in your database

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                G 1 Reply Last reply
                                1
                                • VRoninV VRonin

                                  sorry my bad, it should be:

                                  connect(this,SIGNAL(recAdded()),this,SLOT(updateTable ()));

                                  then call emit recAdded() after you added the record in your database

                                  G Offline
                                  G Offline
                                  gabor53
                                  wrote on last edited by
                                  #16

                                  @VRonin
                                  I modified like this:

                                   querys.prepare("INSERT INTO Items (ID, Name, Pic, Description, Month, Day, Year, History, Age, Notes, Color, Material, Signed, What) VALUES(:ID, :Name, :Pic, :Description, :Month, :Day, :Year, :History, :Age, :Notes, :Color, :Material, :Signed, :What)" );
                                      querys.bindValue (":ID",sIDReview);
                                      querys.bindValue (":Name",nameReview);
                                      querys.bindValue (":Pic",fileByteArray);
                                      querys.bindValue (":Description",descriptionReview);
                                      querys.bindValue (":Month",monthReview);
                                      querys.bindValue (":Day",dayReview);
                                      querys.bindValue (":Year",yearReview);
                                      querys.bindValue (":History",historyReview);
                                      querys.bindValue (":Age",ageReview);
                                      querys.bindValue (":Notes",notesReview);
                                      querys.bindValue (":Color",colorReview);
                                      querys.bindValue (":Material",materialReview);
                                      querys.bindValue (":Signed",SignedbyReview);
                                      querys.bindValue (":What",whatReview);
                                  
                                      bool result = querys.exec ();
                                  
                                      if(!result)
                                          {
                                              qDebug() <<"Error inserting into the main db!" << querys.lastError ();
                                  
                                              QMessageBox::warning (this,"Add to Database Warning","<b><font size='16' color='red'>Error 1002: The Friend was not added to the database.");
                                  
                                          }
                                      else
                                          {
                                              qDebug() << "Entered FunctAdd OK loop.";
                                  
                                  
                                              QMessageBox::information (this,"Confirmation","<b><font size = '16' color = 'green'>The Friend was added to the database.</font>");
                                  
                                              emit RecAdded ();
                                          }
                                  
                                     connect(this,SIGNAL(recAdded()),this,SLOT(updateTable ()));
                                  
                                  }
                                  
                                  void Review::on_pushButton_Fix_clicked()
                                  {
                                      Additem *mAdditem = new Additem(this);
                                      mAdditem->FixFriend (sIDReview, nameReview, whatReview, fileNameReview, materialReview, colorReview, descriptionReview, monthReview, dayReview, yearReview, SignedbyReview, historyReview, ageReview, notesReview);
                                  
                                      mAdditem->show ();
                                  }
                                  
                                  void Review::updateTable()
                                  {
                                      qDebug() << "UpdateTable triggered!";
                                  
                                      MainWindow *myMainWindow = new MainWindow(this);
                                      myMainWindow->Addview ();
                                      myMainWindow->show ();
                                  }
                                  
                                  

                                  It still says
                                  QObject::connect: No such signal Review::recAdded() in ..\Folkfriends_1_0\review.cpp:125
                                  QObject::connect: (sender name: 'Review')
                                  QObject::connect: (receiver name: 'Review')

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

                                    2 problems:

                                    • it's case sensitive
                                    • the connect "must" be done in the constructor

                                    so in the Review constructor put:

                                    connect(this,&Review::RecAdded,this,&Review::updateTable);
                                    

                                    I used the new connect sintax as it's safer

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    1 Reply Last reply
                                    1
                                    • G Offline
                                      G Offline
                                      gabor53
                                      wrote on last edited by
                                      #18

                                      This works> Thank you very much.

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

                                        Still, why not change the model to use a more adequate ?

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

                                        G 1 Reply Last reply
                                        0
                                        • SGaistS SGaist

                                          Still, why not change the model to use a more adequate ?

                                          G Offline
                                          G Offline
                                          gabor53
                                          wrote on last edited by
                                          #20

                                          @SGaist
                                          I will. Actually I'm doing it now.

                                          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