Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML C++ enum as property of QAbstractListModel
QtWS25 Last Chance

QML C++ enum as property of QAbstractListModel

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlenum
5 Posts 2 Posters 2.0k 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.
  • D Offline
    D Offline
    dmoviolin
    wrote on 24 Jul 2018, 08:24 last edited by
    #1

    Hi I want to pass Enum as a property of object here what I got:

    enum TypeData
        {
            TYPEA,
            TYPEB,
            TYPEC
        };
        Q_ENUM(TypeData)
    

    My model passes structure so I got

    Struct{
    //OTHER STUFF
    TypeData MyType;
    };
    I also added it in rolenames, return QVariant.
    Now I want to do something like in delegate
    if(rolenamefortype === mymodel.TYPEA)
        //do stuff
    It is of course registered in main as model worked before this change
    

    It did not work.
    Printing out mymodel.TYPEA gives undefined. Printing rolenamefortype gives random numbers.

    J 1 Reply Last reply 24 Jul 2018, 08:41
    0
    • D dmoviolin
      24 Jul 2018, 08:24

      Hi I want to pass Enum as a property of object here what I got:

      enum TypeData
          {
              TYPEA,
              TYPEB,
              TYPEC
          };
          Q_ENUM(TypeData)
      

      My model passes structure so I got

      Struct{
      //OTHER STUFF
      TypeData MyType;
      };
      I also added it in rolenames, return QVariant.
      Now I want to do something like in delegate
      if(rolenamefortype === mymodel.TYPEA)
          //do stuff
      It is of course registered in main as model worked before this change
      

      It did not work.
      Printing out mymodel.TYPEA gives undefined. Printing rolenamefortype gives random numbers.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 24 Jul 2018, 08:41 last edited by
      #2

      @dmoviolin
      hi, sadly, theres more to it.

      I woudl suggest reading through this blockpost:
      https://www.embeddeduse.com/2017/09/15/passing-enum-properties-between-c-and-qml/

      It's more up to date than my other bookmarked links.

      most likly, from what I've read in your OP, you haven't registered the enum via qmlRegisterUncreatableType and or forgotten to import the registered type in the qml file where you want to use it.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      D 1 Reply Last reply 24 Jul 2018, 09:00
      0
      • J J.Hilk
        24 Jul 2018, 08:41

        @dmoviolin
        hi, sadly, theres more to it.

        I woudl suggest reading through this blockpost:
        https://www.embeddeduse.com/2017/09/15/passing-enum-properties-between-c-and-qml/

        It's more up to date than my other bookmarked links.

        most likly, from what I've read in your OP, you haven't registered the enum via qmlRegisterUncreatableType and or forgotten to import the registered type in the qml file where you want to use it.

        D Offline
        D Offline
        dmoviolin
        wrote on 24 Jul 2018, 09:00 last edited by
        #3

        I have enum inside class that is registeredtype. Do I need to declare enum outside and register it again?

        J 1 Reply Last reply 24 Jul 2018, 09:09
        0
        • D dmoviolin
          24 Jul 2018, 09:00

          I have enum inside class that is registeredtype. Do I need to declare enum outside and register it again?

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 24 Jul 2018, 09:09 last edited by
          #4

          @dmoviolin how did you register it? There are multiple ways

          and AFAIK Enums require qmlRegisterUncreatableType
          Registering c types with the qml type system

          registers a named C++ type that is not instantiable but should be identifiable as a type to the QML type system. This is useful if a type's enums or attached properties should be accessible from QML but the type itself should not be instantiable.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dmoviolin
            wrote on 24 Jul 2018, 09:18 last edited by dmoviolin
            #5

            I registered it as RegiterType cause it is model derived from QAbstractListModel, enum is inside it. I would like to keep enum inside it rather than new header

            Update:
            I can do if(rolenamefortype === 0) etc, cause it's int based like C style enum. However I would like to do if(rolenamefortype === model.TYPEA) cause of clarity

            1 Reply Last reply
            0

            5/5

            24 Jul 2018, 09:18

            • Login

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