Solved Event enter activated two times on aQSpinBox, Crash Program
-
Hello,
I have made a QTableView with a specific collumn formated with QStyledItemDelegate, transforming it in a QSpingBox and I used the signal editingFinished to connect to other slot wich I did. If the user just click in another point of my dialog after editing the QSpinBox the program works correctly, but if he press enter key, he catch the value before the change of editingFinished and the value after the editingFinished, it seems like activated the enter event two times in a row with diferent values and crash my program, I don't know how to fix it.
Only_Number_Class.h
#ifndef ONLY_NUMBER_CLASS_H #define ONLY_NUMBER_CLASS_H class Only_Number : public QStyledItemDelegate { Q_OBJECT protected: QLineEdit *Txt_Total_Compra; QStandardItemModel *Model_Car_Compra; QTableView *Tbv_Car_Compra; public: QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void Update_Txt_Total(); Only_Number(QLineEdit *Txt_C, QStandardItemModel *Mod_C, QTableView *Tbv_C){ Txt_Total_Compra = Txt_C; Model_Car_Compra = Mod_C; Tbv_Car_Compra = Tbv_C; }; }; #endif // ONLY_NUMBER_CLASS_H
Only_Number.cpp
#include "only_number_class.h" #include "venda.h" QWidget *Only_Number::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QSpinBox *Spin_Box = new QSpinBox(parent); connect(Spin_Box, &QSpinBox::editingFinished, this, &Only_Number::Update_Txt_Total); return Spin_Box; } void Only_Number::Update_Txt_Total(){ double Total_Compra = 0; int Id_Produto = 0; int Qtd_Compra = 0; int Qtd_Estoque = 0; for (int row = 0; row < Model_Car_Compra->rowCount(); row++){ Total_Compra += Model_Car_Compra->item(row, 3)->text().toDouble() * Model_Car_Compra->item(row, 4)->text().toDouble(); } ... while (query.next()){ Qtd_Estoque = query.value(0).toInt(); } if ((Qtd_Estoque - Qtd_Compra) < 0) { QMessageBox::information(Tbv_Car_Compra, "Aviso", "Quantidade indisponível em estoque"); return; } QString Formated_Num = QString::number(Total_Compra, 'f', 2); Txt_Total_Compra->setText(Formated_Num); }
The error happen in this stretch.
if ((Qtd_Estoque - Qtd_Compra) < 0) { QMessageBox::information(Tbv_Car_Compra, "Aviso", "Quantidade indisponível em estoque"); return; }
Thanks in advance.
-
@Ritch editingFinished is emitted when spin box loses focus or enter is pressed, so I think this is expected.
Your Update_Txt_Total should simply check whether the value changed compared to last time it was called. -
Thank you for reply,
I think that's really the problem, but I just changed my aproach to solve this. If I enter in this sentense:
if ((Qtd_Estoque - Qtd_Compra) < 0) { QMessageBox::information(Tbv_Car_Compra, "Aviso", "Quantidade indisponível em estoque"); return; }
I just put the zero value in the cell and the problem is solved.
-
Hi,
Out of curiosity, what version of Qt are you currently using ?
-
Hi, my version is 5.9.9, I saw in other posts wich this bug was solved in 5.14 version, it's correct?
-
If your issue relates to this Qt bug report, then yes.