Strange autoclicking of qpushbutton
-
I've been trying to learn c++ and qt framework by doing a point of sale software project.my main window has couple of tabs and with in one tab i have a qpushbutton which open a newpurchase dialog which takes input from user to record a new purchase from supplier.this newpurchase dialog has a lineedit which, when recieves product barcode from a barcode reader ,triggers another quanity dialog which, user can use to enter the quantity of the product. but when i click ok on the button box of the quantity dialog instead of closing just the quantity dialog ,it closes the newpurchase dialog as well.i debugged the qt application and it shows that a qpushbutton (which saves and closes the newpurchase dialog)on the newpuchase dialog gets triggered automatically.additionally this behaviour is only spotted when quantity dialog is triggered through the barcode lineedit,i have another lineedit which accepts description of the product and triggers the same quantity dialog ,but clicking ok button when quantity dialog is triggered through description lineedit does not closes the newpurchase dialog or trigger the qpushbutton which saves and closes the newpurchase dialog.im attaching the screen shots of the dialog and code for the dialogs
on mainwindow.cpp
void MainWindow::on_pushButton_clicked() { newpurchase mypurchase; connect(&mypurchase,SIGNAL(purchase_added()),this,SLOT(update_view())); mypurchase.exec(); }
newpurchase dialog
newpurchase.cpp#include "newpurchase.h" #include "ui_newpurchase.h" #include <QtDebug> #include <qtablewidget.h> #include <QSqlQuery> #include <QSqlError> #include <QSqlRecord> #include <QCompleter> #include <QObject> #include <QWidget> #include <QDate> newpurchase::newpurchase(QWidget *parent) : QDialog(parent), ui(new Ui::newpurchase) { ui->setupUi(this); QSqlQuery qry; qry.prepare("select description from product"); if(!qry.exec()) { qDebug() << "error getting description from product"; } QStringList items; while(qry.next()) { items <<qry.value(0).toString(); } QCompleter *completer =new QCompleter(items,this); completer->setCaseSensitivity(Qt::CaseInsensitive); ui->lineEdit_descripion->setCompleter(completer); connect(completer,static_cast<void (QCompleter::*)(const QString&)>(&QCompleter::activated), [&](const QString &text)->void { newpurchase::clear(text); }); QSqlQuery supqry; supqry.prepare("select supplier_name from supplier"); if(!supqry.exec()) { qDebug() << "error getting suppliername from supplier"; } QStringList supitems; while(supqry.next()) { supitems <<supqry.value(0).toString(); } QCompleter *supcompleter =new QCompleter(supitems,this); supcompleter->setCaseSensitivity(Qt::CaseInsensitive); ui->lineEdit_sup->setCompleter(supcompleter); } newpurchase::~newpurchase() { delete ui; } void newpurchase::on_lineEdit_barcode_returnPressed() { if(!ui->lineEdit_sale->text().isEmpty() && !ui->lineEdit_sup->text().isEmpty() && !ui->lineEdit_inv->text().isEmpty()) { quantity_dialog quandialog; connect(&quandialog,SIGNAL(purdetails(QString)),this,SLOT(code(QString))); quandialog.exec(); } else { Errdialog myerror("please provide supplier and invoice" ); myerror.exec(); } } void newpurchase::saveDataIntoTable(QString item,double price,int quantity,QString code,QString date) { double rupee =price*quantity*1.00; //qDebug() <<date; QString mrp= QString::number(rupee,'f',2); QString qty= QString::number(quantity); QString prc= QString::number(price); // QString kod= QString::number(code); if (!ui->tableWidget) return; const int currentRow = ui->tableWidget->rowCount(); ui->tableWidget->setRowCount(currentRow + 1); ui->tableWidget->setItem(currentRow, 0, new QTableWidgetItem(item)); ui->tableWidget->setItem(currentRow, 1, new QTableWidgetItem(prc)); ui->tableWidget->setItem(currentRow, 2, new QTableWidgetItem(qty)); ui->tableWidget->setItem(currentRow, 3, new QTableWidgetItem(mrp)); ui->tableWidget->setItem(currentRow, 4, new QTableWidgetItem(code)); ui->tableWidget->setItem(currentRow, 5, new QTableWidgetItem(date)); double total=ui->label_3->text().toDouble(); total = total+rupee; ui->label_3->setText(QString::number(total,'f',2)); } void newpurchase::clear(QString item) { if(!item.isEmpty()) { if(!ui->lineEdit_des->text().isEmpty() && !ui->lineEdit_sup->text().isEmpty() && !ui->lineEdit_inv->text().isEmpty()) { quantity_dialog quandialog; connect(&quandialog,SIGNAL(purdetails(QString)),this,SLOT(descr(QString))); quandialog.exec(); } else { Errdialog myerror("please provide supplier and invoice" ); myerror.exec(); } } } void newpurchase::descr(QString q) { QStringList elements = q.split(':'); QSqlQuery qry; qry.prepare("select * from product where description='"+ui->lineEdit_des->text()+"'"); if(!qry.exec()) { qDebug() << "error getting table product"; } if (qry.next()) { QString result=qry.value(1).toString(); double cost= elements[1].toDouble(); QString itemco=qry.value(2).toString(); int quan=elements[0].toInt(); QString mfd=elements[2]; saveDataIntoTable(result,cost,quan,itemco,mfd); ui->lineEdit_des->clear(); } } void newpurchase::on_pushButton_clicked() { QItemSelectionModel *select =ui->tableWidget->selectionModel(); int row = select->selectedRows().takeFirst().row(); double rupee=ui->tableWidget->item(row,3)->text().toDouble(); ui->tableWidget->removeRow(row); double total=ui->label_3->text().toDouble(); total = total-rupee; ui->label_3->setText(QString::number(total,'f',2)); } void newpurchase::on_lineEdit_des_returnPressed() { } void newpurchase::code(QString q) { QStringList elements = q.split(':'); QSqlQuery qry; qry.prepare("select * from product where code="+ui->lineEdit_sale->text()); if(!qry.exec()) { qDebug() << "error getting table product"; } if (qry.next()) { QString result=qry.value(1).toString(); double cost= elements[2].toDouble(); QString itemco=qry.value(2).toString(); int quan=elements[0].toInt(); QString mfd=elements[1]; saveDataIntoTable(result,cost,quan,itemco,mfd); } ui->lineEdit_sale->clear(); } void newpurchase::on_pushButton_2_clicked() { QString datetime= QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"); QString supplier=ui->lineEdit_sup->text(); QString invoice=ui->lineEdit_inv->text(); int numrow=ui->tableWidget->rowCount(); // qDebug() << numrow; for (int i = 0; i < numrow; i++) { QSqlQuery query; query.prepare("SELECT MAX(Id) FROM stock_transaction"); if(!query.exec()) { qDebug() << "error getting id"; } int id=0 ; if (query.next()) { id=query.value(0).toInt()+1; } QString coder=ui->tableWidget->item(i,4)->text(); QString qua=ui->tableWidget->item(i,2)->text(); double rate=ui->tableWidget->item(i,1)->text().toDouble(); QString date= ui->tableWidget->item(i,5)->text(); QString d=QString::number(id); QString batch=supplier+"_"+invoice+"_"+d; QSqlQuery querysale; querysale.prepare("INSERT INTO stock_transaction(id,code,stock,mfd,supplier,invoice,cost,date_time) VALUES(:id,:code,:stock,:mfd,:supplier,:invoice,:cost,:date_time)"); querysale.bindValue(":id", id); querysale.bindValue(":code",coder); querysale.bindValue(":stock", qua.toInt()); querysale.bindValue(":mfd",date); querysale.bindValue(":supplier", supplier); querysale.bindValue(":invoice", invoice); querysale.bindValue(":cost", rate*1.00); querysale.bindValue(":date_time", datetime); if(!querysale.exec()) { qDebug() << "error recording sale"; } } emit this->purchase_added(); close(); }
quantity dialog
quantity.cpp
#include "quantity_dialog.h" #include "ui_quantity_dialog.h" quantity_dialog::quantity_dialog(QWidget *parent) : QDialog(parent), ui(new Ui::quantity_dialog) { ui->setupUi(this); ui->lineEdit_quan->setFocus(); ui->buttonBox->setEnabled(false); connect(ui->lineEdit_quan, SIGNAL(textChanged(QString)), this, SLOT(checkLineEdits())); connect(ui->dateEdit, SIGNAL(dateChanged(QDate)), this, SLOT(checkLineEdits())); connect(ui->lineEdit_3, SIGNAL(textChanged(QString)), this, SLOT(checkLineEdits())); } quantity_dialog::~quantity_dialog() { delete ui; } void quantity_dialog::on_buttonBox_accepted() { emit purdetails(ui->lineEdit_quan->text()+":"+ui->dateEdit->text()+":"+ui->lineEdit_3->text()); ui->lineEdit_quan->clear(); }
Please let me know if i can provide any other details
-
@jerin-johnson said in Strange autoclicking of qpushbutton:
i debugged the qt application and it shows that a qpushbutton (which saves and closes the newpurchase dialog)on the newpuchase dialog gets triggered automatically.additionally this behaviour is only spotted when quantity dialog is triggered through the barcode lineedit
The only way this could happen is, that your button has
default
orautoDefault
property set. So youEnter
inside your dialog to accept your inputs, but it also triggers your default button which apparently saves and closes your dialog. -
@Pl45m4 thanks you so much ,autodefault was set.any idea how this property get set by default?.i recently moved from windows to ubuntu.
-
This property's default is true for buttons that have a QDialog parent; otherwise it defaults to false.
https://doc.qt.io/qt-5/qpushbutton.html#autoDefault-prop
Your buttonBox is a child of your
QDialog
, so it must be true. You can disable it manually, but then you can'taccept
your dialog by pressing enter anymore. But it seems that you don't need this anyway.