Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] How to set enum as dynamic property?

    General and Desktop
    3
    4
    1555
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      Banshee last edited by

      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.]

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        IIRC, you need to

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

        Otherwise you might be trying to convert an int

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • dheerendra
          dheerendra Qt Champions 2022 last edited by

          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;
          }@

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply Reply Quote 0
          • B
            Banshee last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post