PDA

View Full Version : QPushButton,Several unclicked clicks too many



thefatladysingsopera
15th August 2011, 16:24
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?.

mvuori
15th August 2011, 16:36
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.)

duma
15th August 2011, 20:54
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.

thefatladysingsopera
16th August 2011, 07:00
Here is the code,i couldn't post yesterday since i was away from my computer.


#include <QtGui>
#include <QtSql>
#include "smith.h"

smith::smith(QWidget *parent) :
QWidget(parent)
{
ui.setupUi(this);
setupModel();

//tables Start
tablesModel = new QSqlTableModel(ui.tables);
tablesModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
tablesModel->setTable("tables");

tablesModel->select();

ui.tables->setModel(tablesModel);

ui.tables->setColumnHidden(tablesModel->fieldIndex("id"), true);

ui.tables->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui.tables->setSelectionMode(QAbstractItemView::SingleSelectio n);

ui.tables->setSortingEnabled(true);

ui.tables->setSelectionBehavior(QAbstractItemView::SelectRows );
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(),
SIGNAL(currentRowChanged(const QModelIndex &,
const QModelIndex &)),
this, SLOT(updateView()));

//tables end
}


{
...
//UPDATE VIEW FUNCTION
tableModel->select();
//qt center question starts here
ui.tableView->setModel(tableModel);
ui.tableView->setColumnHidden(tableModel->fieldIndex("id"), true);

ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui.tableView->setSelectionMode(QAbstractItemView::SingleSelectio n);

ui.tableView->setSortingEnabled(true);

ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows );
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
QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(tableModel);
mapper->addMapping(ui.fEdit, 1);
mapper->addMapping(ui.lnEdit, 2);
mapper->addMapping(ui.cEdit, 3);
mapper->addMapping(ui.cityEdit, 4);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->toFirst();


connect(ui.tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
mapper, SLOT(setCurrentModelIndex(QModelIndex)));

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()));
}

stampede
16th August 2011, 07:55
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).

nish
16th August 2011, 09:02
pass Qt::UniqueConnection to the connect statement to avoid duplicates.

thefatladysingsopera
16th August 2011, 09:51
Thanks all.I have used
connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()), Qt::UniqueConnection); and works well.