Skip to content

Instantly share code, notes, and snippets.

@martonmiklos
Last active February 21, 2021 11:18
Show Gist options
  • Save martonmiklos/81e9b9dbeab1787e81e08a2b68c098b9 to your computer and use it in GitHub Desktop.
Save martonmiklos/81e9b9dbeab1787e81e08a2b68c098b9 to your computer and use it in GitHub Desktop.
QComboBox wit autofill from QMetaEnum
// see https://github.com/martonmiklos/qt_goodies/blob/master/qmetaenumcombobox.h for more features
#pragma once
#include <QObject>
#include <QWidget>
#include <QVariant>
#include <QComboBox>
#include <QDebug>
#include <QMetaEnum>
template<typename EnumType>
class QMetaEnumComboBox : public QComboBox
{
public:
QMetaEnumComboBox(QWidget *parent = nullptr) :
QComboBox(parent)
{
QMetaEnum metaEnum = QMetaEnum::fromType<EnumType>();
for(int i = 0; i<metaEnum.keyCount(); i++)
addItem(metaEnum.valueToKey(metaEnum.value(i)), metaEnum.value(i));
}
void setCurrentEnumValue(EnumType value)
{
setCurrentIndex(findData(static_cast<int>(value)));
}
EnumType currentEnumValue() const
{
return static_cast<EnumType>(currentData().toInt());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment