Here we go for the second approach (subclass QComboBox). I recommend the first approach (QSignalMapper), though.
Disclaimer: I did not even try to compile this code, there may be errors.
Q_OBJECT
public:
MyComboBox
(int id,
QWidget *parent
= 0);
signals:
void currentIndexChanged(int index, int id);
protected slots:
void onCurrentIndexChanged(int index);
protected:
int m_id;
};
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(onCurrentIndexChanged(int)));
}
void MyComboBox::onCurrentIndexChanged(int index) {
emit currentIndexChanged(index, m_id);
}
class MyComboBox : public QComboBox {
Q_OBJECT
public:
MyComboBox(int id, QWidget *parent = 0);
signals:
void currentIndexChanged(int index, int id);
protected slots:
void onCurrentIndexChanged(int index);
protected:
int m_id;
};
MyComboBox::MyComboBox(int id, QWidget *parent) : QComboBox(parent), m_id(id) {
connect(this, SIGNAL(currentIndexChanged(int)), SLOT(onCurrentIndexChanged(int)));
}
void MyComboBox::onCurrentIndexChanged(int index) {
emit currentIndexChanged(index, m_id);
}
To copy to clipboard, switch view to plain text mode
Signals are not simple methods (although they are implemented by moc as methods). They are emitted by a component and you need to connect them to a slot to react to their emission.
Bookmarks