5
votes

I am writing a small program which uses Qt5 QML as the GUI layer and Python3-PyQt5 to implement the data model.

I now want to display a ComboBox in QML and set its model to a list of enums. How would I export the enum as a property of the python class so that I can reference it in QML?

Preferably I would write this in QML:

ComboBox {
  model: mymodel.car_manufacturers
  onCurrentIndexChanged: mymodel.selected_manufacturer = currentIndex
}
2

2 Answers

2
votes

Here is my solution which works good enough for me. In the python code I have the following:

class CarManufacturers(enum.Enum):
    BMW, Mercedes = range(2)

mfcChanged = pyqtSignal()

@pyqtProperty('QStringList', constant=True)
def carmanufacturers(self):
    return [mfc.name for mfc in CarManufacturers]

@pyqtProperty('QString', notify=mfcChanged)
def mfc(self):
    return str(CarManufacturers[self._mfc].value)

@modus.setter
def mfc(self, mfc):
    print('mfc changed to %s' % mfc)
    if self._mfc != CarManufacturers(int(mfc)).name:
        self._mfc = CarManufacturers(int(mfc)).name
        self.mfcChanged.emit()

and in the QML I have:

ComboBox {
    model: myModel.carmanufacturers
    currentIndex: myModel.mfc
    onCurrentIndexChanged: myModel.mfc = currentIndex
}
0
votes

You should put the enum in your class that derives from QObject. Also it should be marked with Q_ENUMS macro. You can then take the QMetaEnum of your enum from the metaobject of the class, iterate through the keys and their values and add each one to a QStringList.

In C++ it would be like :

MyClass myObj;
const QMetaObject* metaObj = myObj.metaObject();
QMetaEnum enumType = metaObj->enumerator(metaObj->indexOfEnumerator("MyEnumType"));

QStringList list;
for(int i=0; i < enumType.keyCount(); ++i)
{
    QString item = QString::fromLatin1(enumType.key(i)) + "  "
                 + QString::number(enumType.value(i));
    list.append(item);
}

Now you can use QQmlContext::setContextProperty to expose the the string list to QML :

QQmlContext *context = engine.rootContext();
context->setContextProperty("myModel", QVariant::fromValue(list));

You would have a combo-box containing the enum key and values when the ComboBox item is like :

ComboBox {
  model: myModel
  ...
}