Get the previous QComboBox value that is in the QTableWidget when the value is changed

Let's say I have a QTableWidget, and each line has a QComboBox and a QSpinBox. Note that I store their QMap theMap values;

When the value of value or spin boxes of the value of comboBoxes changes, I want to update the "theMap". Therefore, I must know what the previous value of the combo box was in order to replace the new value of the combo box, and also take care of the value of the combo box.

How can i do this?

PS I decided to create a slot that, when I click on the table, saves the current value of the combo box of this row. But this only works when you click on the title bar. In other places (clicking on the combo box or on the formatting field) the itemSelectionChanged () QTableWidget signal does not work. Thus, in general, my problem is to save the value of the combo box of the selected row, and I will get a ComboBox or SpinBox, and you will easily handle "TheMap".

+4
source share
3 answers

How to create your own, received QComboBox class, something like:

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 old combo text.

I hope this helps.

+6
source

A bit late, but I had the same problem and solved 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; }; 
+4
source

My suggestion is to implement a model that will help you make a clear separation of the data and a user interface that edits the data. Then your model will be notified that this model index (row and column) has changed to new data, and you could change any other data that you need at this point.

0
source

Source: https://habr.com/ru/post/1314812/


All Articles