Important: Please read the Qt Code of Conduct -

[SOLVED] Enum with QList and Pointers

  • Hi, I'm currently trying to convert my QList<QString*> to an enum version so it can be faster.

    I would like to do the following, QList<EnumType*>. I am wondering if there is any reason I shouldn't do this. I could really benefit from the pointer in the QList for my next step which is to convert it to a QQmlListProperty<EnumType> which takes a QList<EnumType*>.

  • Moderators


    How do you plan to use your list? I'd use strings instead of enums if I want to generate the type names dynamically. Otherwise, I don't see why you shouldn't change to enums.

    However, you should generally store both enums and QStrings as plain values instead of pointers, for the same reason you'd store integer values instead of integer pointers. QStrings are "implicitly shared":, so copying them is a very efficient process (unlike strcpy() or strdup()).

  • QQmlListProperty has a constructor for a list of pointers, I supposed I can just create the functions required for it to work when it's not a list of pointers and just a list of "primitives" but was hoping to avoid the hassle.

    The purpose is to get access to the QList within QML. I also want it to be an enum as it will be used in an AI algorithm that will copy data quite often as well as comparing it. Unless QString comparisons can be done just as quickly I would prefer to save as much time as I can during computation.

  • Moderators

    You'll need to find a different approach, unfortunately. QQmlProperty does not work on strings or enums. From the "QQmlListProperty documentation":
    "QQmlListProperty can only be used for lists of QObject-derived object pointers."

    What do you with to do with your list in QML?

    Integer comparisons are always faster than string comparisons (well, the two might be equal if your strings all start with different characters).

  • Changing around how I've set up my code for this to make it easier. Marking as solved in the mean time.

  • Moderators

    I saw your other thread: Yes, having a Pet class that contains all the pet attributes that is the usual way to do it in OOP.

    All the best with your project.

Log in to reply