Last active
February 21, 2021 11:18
-
-
Save martonmiklos/81e9b9dbeab1787e81e08a2b68c098b9 to your computer and use it in GitHub Desktop.
QComboBox wit autofill from QMetaEnum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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