Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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<QString /*Combo box val*/,int /*spin box val*/> theMap;

When comboBoxes value or spin boxes value is being changed I want to update theMap. So I should know what was the former value of the combo box in order to replace with the new value of the comboBox and also take care of the value of the spin box.

How can I do this?

P.S. I have decided to create a slot that when you click on a table, it stores the current value of the combo box of that row. But this works only when you press on row caption. In other places (clicking on a combobox or on a spinbox) itemSelectionChanged() signal of QTableWidget does not work.

So in general my problem is to store the value of the combo box of selected row, and the I will get ComboBox or SpinBox change even and will process theMap easily.

like image 772
Narek Avatar asked Jul 06 '10 07:07

Narek


1 Answers

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.

like image 127
Greg S Avatar answered Oct 13 '22 21:10

Greg S