Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Event enter activated two times on aQSpinBox, Crash Program
Forum Updated to NodeBB v4.3 + New Features

Event enter activated two times on aQSpinBox, Crash Program

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 482 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ritch
    wrote on last edited by Ritch
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • R Ritch

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • R Offline
        R Offline
        Ritch
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Out of curiosity, what version of Qt are you currently using ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Ritch
            wrote on last edited by
            #5

            Hi, my version is 5.9.9, I saw in other posts wich this bug was solved in 5.14 version, it's correct?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              If your issue relates to this Qt bug report, then yes.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved