views:

43

answers:

2

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 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 combo box 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 combo box or on a spin box) 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.

+1  A: 

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.

Greg S
This is fine of course, but how I can understand which rows combo box (or spin box) has been edited?
Narek
A: 

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.

Caleb Huitt - cjhuitt