[Solved] How to set enum as dynamic property?



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

    And i set the property in the source:
    @
    QNetworkReply* reply=this->get(url);
    reply->setProperty("reqPos",OnCheckSync);
    requestPosition pos= reply->property("reqPos").value<requestPosition>();
    if(pos==OnCheckSync)
    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

    Hi,

    IIRC, you need to

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

    Otherwise you might be trying to convert an int



  • This should help you.

    @enum PersonRoles {
    NameRole = Qt::UserRole + 1,
    IdRole,
    PinRole,
    AddressRole
    };

    Widget w;
    w.setProperty("abc",IdRole);
    int role = w.property("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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.