Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to set enum as dynamic property?

[Solved] How to set enum as dynamic property?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.8k Views
  • 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 Offline
    B Offline
    Banshee
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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
        0
        • B Offline
          B Offline
          Banshee
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved