tableView update content
-
I have a tablewView.I also have a dialog which parent is the QWidgets containing the tableView.
When enter a data in the dialog and click a button "ADD" i insert new row in the SQLite db.This db is connected to the tableView.I need to update also the tableView.
This is the dialog:
#include "partnersadddialog.h" #include "ui_partnersadddialog.h" #include <QSqlQuery> #include <QSqlQueryModel> #include <QSqlTableModel> #include <QSqlError> PartnersAddDialog::PartnersAddDialog(QWidget *parent) : QDialog(parent), ui(new Ui::PartnersAddDialog) { ui->setupUi(this); }
PartnersAddDialog::~PartnersAddDialog() { delete ui; } void PartnersAddDialog::on_pushButton_add_clicked() {
QSqlQuery query; query.prepare("INSERT INTO partners (Name, City, Address, MOL, Bulstat, VAT, Phone, eMail)" "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); query.addBindValue(ui->lineEdit_name->text()); query.addBindValue(ui->lineEdit_city->text()); query.addBindValue(ui->lineEdit_address->text()); query.addBindValue(ui->lineEdit_mol->text()); query.addBindValue(ui->lineEdit_bulstat->text()); query.addBindValue(ui->lineEdit_vat->text()); query.addBindValue(ui->lineEdit_tel->text()); query.addBindValue(ui->lineEdit_mail->text());
query.exec(); emit updateTable(); this->close(); }
void PartnersAddDialog::on_pushButton_cancel_clicked() { this->close(); } This is the QWidget with the table: Partners::Partners(QWidget *parent) : QWidget(parent), ui(new Ui::Partners) { ui->setupUi(this);
QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("SELECT Name, City, Bulstat FROM partners"); model->setHeaderData(0, Qt::Horizontal, QObject::tr("Име")); model->setHeaderData(1, Qt::Horizontal, QObject::tr("Град")); model->setHeaderData(2, Qt::Horizontal, QObject::tr("ЕИК:"));
ui->tableView_partners->setModel(model); ui->tableView_partners->setColumnWidth(0,299); ui->tableView_partners->setColumnWidth(1,225); ui->tableView_partners->setColumnWidth(2,225); ui->tableView_partners->horizontalHeader()->sectionResizeMode(QHeaderView::Interactive); ui->tableView_partners->horizontalHeader()->setStretchLastSection(true); }
Partners::~Partners() { delete ui; } void Partners::on_pushButton_new_clicked() { PartnersAddDialog *d = new PartnersAddDialog(this); d->show(); }
void Partners::updateTable() { ui->tableView_partners->repaint(); qDebug("called"); }
-
Hi and welcome to devnet
Do you have an event loop running?
AFAIK the update should be automatically then. Anyway you can call the widget update, but this requires also that an event loop is running.PS: I have added markdown tags in your code section. This makes the code more readable. in the forum. You find the major tags at the end of this page.
-
Thank you for your answer.
i ran this.update(); but this update the Dialog.
I need to update the tableView which belongs to the parent.
Due to the event loop, my main.cpp looks like thisQApplication a(argc, argv);
InvoicePlusPlus w; QDesktopWidget *desktop = QApplication::desktop(); screenWidth = desktop->width(); screenHeight = desktop->height(); x = (screenWidth - WIDTH) / 2; y = (screenHeight - HEIGHT) / 2; w.resize(WIDTH, HEIGHT); w.move( x, y ); w.setWindowTitle("Invoice++"); w.show(); return a.exec();
-
Hi and welcome to devnet,
Since you are doing things low-level, then yes you have to refresh your model. Why not use QSqlTableModel to handle that ? You can hide the columns you don't want do see and do your update from it. That way both your views and your database will be up to date.
-
In my case, how to update the model?If you thing that the model should be updated, no the table.
When i click another tab from my app and get back the table has being updated.It means that the model is updated, but the tableView didn't. -
If you want to update the model directly, you have to re-set the query on it. Again, since you are modifying your table, QSqlTableModel is better suited.
-
If you are still using QSqlQuery to update the database, you have to call select on the model
-
-
Since you switched to QSqlTableModel, are you using it's functions to add new rows ?