Important: Please read the Qt Code of Conduct -

[Solved] How to set enum as dynamic property?

  • Hello,
    I got a custom enum in my header:
    @enum requestPosition{...,LoadMatch,OnCheckMatch,OnCheckSync,...};

    And i set the property in the source:
    QNetworkReply* reply=this->get(url);
    requestPosition pos= reply->property("reqPos").value<requestPosition>();
    qDebug() << "ok";
    QList<QByteArray> dynPropNames=reply->dynamicPropertyNames();
    foreach(QByteArray name,dynPropNames)
    qDebug() << QString::fromLatin1(name);

    The "ok" is not printed,but "reqPos" is listed in the dynPropNames-list. What's wrong with my code? :(
    Thank you all!

    [EDIT: Best ideas always come after submitting...Do I have to subclass QNetworkReply and declare/register the enum in the header file of this new class? I'll try that.]

  • Lifetime Qt Champion


    IIRC, you need to

    @reply->setProperty("reqPos", QVariant::fromValue(OnCheckSync));@

    Otherwise you might be trying to convert an int

  • Qt Champions 2017

    This should help you.

    @enum PersonRoles {
    NameRole = Qt::UserRole + 1,

    Widget w;
    int role ="abc").toInt();
    if (role == IdRole) {
    qDebug() << " I am here" << endl;
    }else {
    qDebug() << role;

  • Thank you, both ways work. I chose the QVariant::fromValue, because it is just one line fore me to edit :)

Log in to reply