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. Casting QList<T> values to subclasses of T

Casting QList<T> values to subclasses of T

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

    Hello, I seem to have coded myself into a corner.

    Let's assume I have a class that publicly inherits QString called NewString

    Now if I do this

    @QList<QString> list;
    list.append(NewString());@

    How do I get my NewString back out?

    @NewString string = list.value(0); // C2440: Cannot convert QString to NewString
    @

    Casting does not work. It will work if I use QList<QString*> instead but I wanted to find a way to do this without pointers. I am using a QList to store a bunch of different implicitly shared objects that share the same base class but I am having difficulty restoring them after I pull them out of the QList.

    Any thoughts would be greatly appreciated! Thanks,
    -Will

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mmoll
      wrote on last edited by
      #2

      It doesn't work.

      What append does in your case is called slicing, i.e. it takes the QString sub-object of your NewString object. The QList never stores a NewString object in the first place.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        What can work is a QList<QString*>
        But you lose all the advantages of implicit sharing.

        You might be better of writing a wrapper class instead of a derived class. Save the QString as one member of your class, and add any other information you want. Of course, it's not as convenient to use, but there's less danger of unintentional misuse, i.e. features that work because the QString subclass provides them, but that don't work right because your class should behave differently.

        1 Reply Last reply
        0
        • O Offline
          O Offline
          oh3netid
          wrote on last edited by
          #4

          Thank you both. I think I'm just going to create a separate QList for each of my subclasses.

          The wrapper idea is interesting but I can't think of an elegant way to make it work for me. I'm implementing a component pattern to store mixed media playlists. So my QList would have ID3 tag info, Video info, and other QLists, all stored as implicitly shared data types (I used QString just to simplify the illustration).

          Anything, thanks for the help!

          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