Skip to content

Instantly share code, notes, and snippets.

@mistic100
Last active December 26, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistic100/cb2ae486ccac982a4699 to your computer and use it in GitHub Desktop.
Save mistic100/cb2ae486ccac982a4699 to your computer and use it in GitHub Desktop.
[Qt/C++] QComboBox With direct access to item data
#ifndef QCOMBOBOXEXT_H
#define QCOMBOBOXEXT_H
#include <QWidget>
#include <QComboBox>
/**
* @brief QComboBox With direct access to item data
* @signal currentDataChanged(QVariant)
*/
class QComboBoxExt : public QComboBox
{
Q_OBJECT
public:
QComboBoxExt(QWidget* _parent = 0) : QComboBox(_parent) {
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(on_currentIndexChanged(int)));
}
void setCurrentData(const QVariant &_data)
{
setCurrentIndex(findData(_data));
}
QVariant currentData()
{
return itemData(currentIndex());
}
signals:
void currentDataChanged(QVariant);
private slots:
void on_currentIndexChanged(int index)
{
emit currentDataChanged(itemData(index));
}
};
#endif // QCOMBOBOXEXT_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment