I have made an application that inserts some data into an sqlite database.The problem is once i click save it inserts several duplicate records instead of just one.What could be the problem?.
Printable View
I have made an application that inserts some data into an sqlite database.The problem is once i click save it inserts several duplicate records instead of just one.What could be the problem?.
There could be a so-called bug in the application...
First thing to check is to find out how many clicks the button really emits. There might be only one and the flaw could be somewhere else, having nothing to do with the pushbutton.
But even if the button misbehaves, your application has a problem if it inserts duplicate records. (This is the traditional problem with badly designed web applicatios where the impatient user clicks the button several times thinking that the click was not received, ending with the form data sent several times, and saved several times.)
I might be way off, but may i suggest disabling your pushbutton once it has been clicked to make sure that only one click occurs. use command:
ui->pushButton->setEnabled(false) to disable it in the pushButton command.
Here is the code,i couldn't post yesterday since i was away from my computer.
Code:
#include <QtGui> #include <QtSql> #include "smith.h" { ui.setupUi(this); setupModel(); //tables Start tablesModel->setTable("tables"); tablesModel->select(); ui.tables->setModel(tablesModel); ui.tables->setColumnHidden(tablesModel->fieldIndex("id"), true); ui.tables->setSortingEnabled(true); ui.tables->horizontalHeader()->setStretchLastSection(true); //localized header captions tablesModel->setHeaderData(1, Qt::Horizontal, tr("Table Name")); tablesModel->setHeaderData(2, Qt::Horizontal, tr("Table Description")); connect(ui.tables->selectionModel(), this, SLOT(updateView())); //tables end }
Code:
{ ... //UPDATE VIEW FUNCTION tableModel->select(); //qt center question starts here ui.tableView->setModel(tableModel); ui.tableView->setColumnHidden(tableModel->fieldIndex("id"), true); ui.tableView->setSortingEnabled(true); ui.tableView->horizontalHeader()->setStretchLastSection(true); //localized header captions tableModel->setHeaderData(1, Qt::Horizontal, tr("First Name")); tableModel->setHeaderData(2, Qt::Horizontal, tr("Last Name")); tableModel->setHeaderData(3, Qt::Horizontal, tr("Country")); tableModel->setHeaderData(4, Qt::Horizontal, tr("City")); //Mapper mapper->setModel(tableModel); mapper->addMapping(ui.fEdit, 1); mapper->addMapping(ui.lnEdit, 2); mapper->addMapping(ui.cEdit, 3); mapper->addMapping(ui.cityEdit, 4); mapper->toFirst(); connect(ui.newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked())); connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(on_cancelButton_clicked())); connect(ui.nextButton, SIGNAL(clicked()), mapper, SLOT(toNext())); connect(ui.previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious())); connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked())); connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked())); }
For each connect statement you will get exactly one slot call on signal emit (even if you click one time, if connect was called twice, the slot will be called twice ), so verify that you didn't connect the button more than once (for example, once by connectSlotsByName and once manually).
pass Qt::UniqueConnection to the connect statement to avoid duplicates.
Thanks all.I have usedand works well.Code:
connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()), Qt::UniqueConnection);