TableView become blank after insert
-
Hey,
I do an insert in a dialog like that :

I click "Ok", and when i close the QDialog to go back to my TableView, it become empty :

Why do i get this ? I want 2 things :- My TableView doesn't became blank
- My table view makes an update when i go back to this window. I can do that by clicking on a button which contains "modelTable->select();", but i want to update it automatically (without the obligation to click on a button to get that).
Thanks !
-
My List of products :
listOfProducts::listOfProducts(QWidget *parent) : QDialog(parent), ui(new Ui::listOfProducts) { ui->setupUi(this); this->setWindowTitle("Page des produits"); QBarSet *set0 = new QBarSet("A"); QBarSet *set1 = new QBarSet("B"); * set0 << 10 << 20 << 30; * set1 << 10 << 20 << 30; QBarSeries *series = new QBarSeries(); series ->append(set0); series ->append(set1); QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Moyenne par an"); chart->setAnimationOptions(QChart::AllAnimations); QStringList categories; categories << "2020" << "2021" << "2022"; QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); //chart->setAxisX(axis, series); chart->legend()->setVisible(true); chart->legend()->setAlignment(Qt::AlignBottom); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setParent(ui->vueDiagramme_2); //ui->vueDiagramme->setModel(chartView); QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry(QSqlDatabase::database("blossom")); modeleTable = new QSqlTableModel(); modeleTable->setTable("produit"); modeleTable->select(); ui->vueTable->setModel(modeleTable); } else { QMessageBox::information(this,"Pas connecté","Vous n'êtes pas connecté à la base de données"); } }And my form to add a product :
void ajouterproduit::on_pushButton_2_clicked() { QString produit_nom = ui->nomproduit->text(); QString produit_quantite = ui->quantite->text(); QString produit_type = ui->typeproduit->text(); qInfo() << produit_nom; qInfo() << produit_quantite; qInfo() << produit_type; QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry; qry.prepare("INSERT INTO produit (produit_nom, produit_quantite, produit_type)" "VALUES (:produit_nom,:produit_quantite,:produit_type)"); qry.bindValue(":produit_nom", produit_nom); qry.bindValue(":produit_quantite", produit_quantite); qry.bindValue(":produit_type", produit_type); if(qry.exec()) { QMessageBox::information(this,"Produit ajouté","Le produit a bien été ajouté."); } else{ QMessageBox::information(this,"Erreur","Le produit n'a pas été ajouté."); } } else { QMessageBox::information(this,"Pas ouverte","pas ouverte"); } }Thanks !
You should not be doing the
addDatabase(), nor thesetHostName()and credentials, nor theopen(), each time inside a button click, and inside a list of products. You should be doing it once outside of these (e.g. somewhere near application startup), and sharing the database access across all your code.After you have done your
INSERTnothing issues a new SQL query to update any existing rows read in or the model.Rewrite your code: just use a
QSqlTableModel, not your own queries. When you insert a new row it will update the database and your boundQTableViewwill see the updates automatically. -
Hey,
I do an insert in a dialog like that :

I click "Ok", and when i close the QDialog to go back to my TableView, it become empty :

Why do i get this ? I want 2 things :- My TableView doesn't became blank
- My table view makes an update when i go back to this window. I can do that by clicking on a button which contains "modelTable->select();", but i want to update it automatically (without the obligation to click on a button to get that).
Thanks !
@Cervo2paille What about posting code?
-
My List of products :
listOfProducts::listOfProducts(QWidget *parent) : QDialog(parent), ui(new Ui::listOfProducts) { ui->setupUi(this); this->setWindowTitle("Page des produits"); QBarSet *set0 = new QBarSet("A"); QBarSet *set1 = new QBarSet("B"); * set0 << 10 << 20 << 30; * set1 << 10 << 20 << 30; QBarSeries *series = new QBarSeries(); series ->append(set0); series ->append(set1); QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Moyenne par an"); chart->setAnimationOptions(QChart::AllAnimations); QStringList categories; categories << "2020" << "2021" << "2022"; QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); //chart->setAxisX(axis, series); chart->legend()->setVisible(true); chart->legend()->setAlignment(Qt::AlignBottom); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setParent(ui->vueDiagramme_2); //ui->vueDiagramme->setModel(chartView); QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry(QSqlDatabase::database("blossom")); modeleTable = new QSqlTableModel(); modeleTable->setTable("produit"); modeleTable->select(); ui->vueTable->setModel(modeleTable); } else { QMessageBox::information(this,"Pas connecté","Vous n'êtes pas connecté à la base de données"); } }And my form to add a product :
void ajouterproduit::on_pushButton_2_clicked() { QString produit_nom = ui->nomproduit->text(); QString produit_quantite = ui->quantite->text(); QString produit_type = ui->typeproduit->text(); qInfo() << produit_nom; qInfo() << produit_quantite; qInfo() << produit_type; QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry; qry.prepare("INSERT INTO produit (produit_nom, produit_quantite, produit_type)" "VALUES (:produit_nom,:produit_quantite,:produit_type)"); qry.bindValue(":produit_nom", produit_nom); qry.bindValue(":produit_quantite", produit_quantite); qry.bindValue(":produit_type", produit_type); if(qry.exec()) { QMessageBox::information(this,"Produit ajouté","Le produit a bien été ajouté."); } else{ QMessageBox::information(this,"Erreur","Le produit n'a pas été ajouté."); } } else { QMessageBox::information(this,"Pas ouverte","pas ouverte"); } }Thanks !
-
My List of products :
listOfProducts::listOfProducts(QWidget *parent) : QDialog(parent), ui(new Ui::listOfProducts) { ui->setupUi(this); this->setWindowTitle("Page des produits"); QBarSet *set0 = new QBarSet("A"); QBarSet *set1 = new QBarSet("B"); * set0 << 10 << 20 << 30; * set1 << 10 << 20 << 30; QBarSeries *series = new QBarSeries(); series ->append(set0); series ->append(set1); QChart *chart = new QChart(); chart->addSeries(series); chart->setTitle("Moyenne par an"); chart->setAnimationOptions(QChart::AllAnimations); QStringList categories; categories << "2020" << "2021" << "2022"; QBarCategoryAxis *axis = new QBarCategoryAxis(); axis->append(categories); chart->createDefaultAxes(); //chart->setAxisX(axis, series); chart->legend()->setVisible(true); chart->legend()->setAlignment(Qt::AlignBottom); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setParent(ui->vueDiagramme_2); //ui->vueDiagramme->setModel(chartView); QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry(QSqlDatabase::database("blossom")); modeleTable = new QSqlTableModel(); modeleTable->setTable("produit"); modeleTable->select(); ui->vueTable->setModel(modeleTable); } else { QMessageBox::information(this,"Pas connecté","Vous n'êtes pas connecté à la base de données"); } }And my form to add a product :
void ajouterproduit::on_pushButton_2_clicked() { QString produit_nom = ui->nomproduit->text(); QString produit_quantite = ui->quantite->text(); QString produit_type = ui->typeproduit->text(); qInfo() << produit_nom; qInfo() << produit_quantite; qInfo() << produit_type; QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL"); database.setHostName("localhost"); database.setDatabaseName("blossom"); database.setUserName("blossom"); database.setPassword("blossom"); if (database.open()) { QSqlQuery qry; qry.prepare("INSERT INTO produit (produit_nom, produit_quantite, produit_type)" "VALUES (:produit_nom,:produit_quantite,:produit_type)"); qry.bindValue(":produit_nom", produit_nom); qry.bindValue(":produit_quantite", produit_quantite); qry.bindValue(":produit_type", produit_type); if(qry.exec()) { QMessageBox::information(this,"Produit ajouté","Le produit a bien été ajouté."); } else{ QMessageBox::information(this,"Erreur","Le produit n'a pas été ajouté."); } } else { QMessageBox::information(this,"Pas ouverte","pas ouverte"); } }Thanks !
You should not be doing the
addDatabase(), nor thesetHostName()and credentials, nor theopen(), each time inside a button click, and inside a list of products. You should be doing it once outside of these (e.g. somewhere near application startup), and sharing the database access across all your code.After you have done your
INSERTnothing issues a new SQL query to update any existing rows read in or the model.Rewrite your code: just use a
QSqlTableModel, not your own queries. When you insert a new row it will update the database and your boundQTableViewwill see the updates automatically.