Get previous value of QComboBox, which is in a QTableWidget, when the value is changed

前端 未结 4 990
长情又很酷
长情又很酷 2021-02-09 16:55

Say I have a QTableWidget and in each row there is a QComboBox and a QSpinBox. Consider that I store their values is a QMap

相关标签:
4条回答
  • 2021-02-09 17:37

    I was just having a similar issue, but for me i needed the previous index for something very trivial so defining and implementing a whole class for it was unjustified.

    So what I did instead was keep an argument called say 'previousIndex' and updated it's value only after I had done everything I needed with it

    0 讨论(0)
  • 2021-02-09 17:39

    How about creating your own, derived QComboBox class, something along the lines of:

    class MyComboBox : public QComboBox
    {
      Q_OBJECT
    private:
      QString _oldText;
    public:
      MyComboBox(QWidget *parent=0) : QComboBox(parent), _oldText() 
      {
        connect(this,SIGNAL(editTextChanged(const QString&)), this, 
            SLOT(myTextChangedSlot(const QString&)));
        connect(this,SIGNAL(currentIndexChanged(const QString&)), this, 
            SLOT(myTextChangedSlot(const QString&)));
      }
    private slots:
      myTextChangedSlot(const QString &newText)
      {
        emit myTextChangedSignal(_oldText, newText);
        _oldText = newText;
      }
    signals:
      myTextChangedSignal(const QString &oldText, const QString &newText);  
    };
    

    And then just connect to myTextChangedSignal instead, which now additionally provides the old combo box text.

    I hope that helps.

    0 讨论(0)
  • 2021-02-09 17:45

    A bit late but I had the same problem and solved in this way:

    class CComboBox : public QComboBox
    {
       Q_OBJECT
    
       public:
          CComboBox(QWidget *parent = 0) : QComboBox(parent) {}
    
    
          QString GetPreviousText() { return m_PreviousText; }
    
       protected:
          void mousePressEvent(QMouseEvent *e)
          { 
             m_PreviousText = this->currentText(); 
             QComboBox::mousePressEvent(e); 
          }
    
       private:
          QString m_PreviousText;
    };
    
    0 讨论(0)
  • 2021-02-09 17:47

    My suggestion is to implement a model, which would help you make a clean separation between the data, and the UI editing the data. Your model would then get notified that a given model index (row and column) changed to the new data, and you could change whatever other data you needed to at that point.

    0 讨论(0)
提交回复
热议问题