Updating QStandardItemModel when records added to QSQLITE database
-
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
-
@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 -
Hi,
Out of curiosity, why not use QSqlTableModel since you are already using QTableView ?
-
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. -
@gabor53 The reason is
connect
expects an object as its first parameter but youron_pushButton_Add_to_DB_clicked()
returns avoid
and thus the errorinvalid use of void expression
Return an object from that function or use an object directly(makes it more easier to understand). -
@VRonin
Thank you. Now I haveconnect(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. -
@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') -
Still, why not change the model to use a more adequate ?