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. How to Create a QList or QVector of mixed types
Forum Updated to NodeBB v4.3 + New Features

How to Create a QList or QVector of mixed types

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 7 Posters 1.6k Views 5 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.
  • L leinad

    Hi,

    I'm trying to create a QLIst or QVector (whichever is easier) to allow me to have mixed types such as 'int' and QGraphicsEllipseItem.

    I tried using QVariant but it does not accept QGraphicsEllipseItem *. Is this possible? I'd like to iterate through each List index for the specific row and column values and use QGraphicsItem as I need to. Once I have the list it should be easier to iterate but I can add mixed types to the list. I suppose I can have two vectors (one for int's and the other for QGraphicsEllipseItem ) but it's easier to maintain one list if I can do it.

    Below is a example of what I'm looking to do.

    List[0]
    int row
    int column
    QGraphicsEllipseItem *
    List[1]
    int row
    int column
    QGraphicsEllipseItem *
    etc

    Thanks!

    artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by artwaw
    #2

    @leinad I think you're looking for a QVector/QList of certain struct that needs to be defined:

    struct yourStruct {
     int row;
     int col;
     QGraphicsEllipseItem *item;
    }
    QVector<yourStruct> data;
    

    ***Corrected an obvious typing error, thanks to @JonB for spotting and sorting me out quickly!

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    3
    • L Offline
      L Offline
      leinad
      wrote on last edited by
      #3

      Great thanks! And I can add and remove to the vector just like any other item in a vector?

      JonBJ artwawA 2 Replies Last reply
      0
      • L leinad

        Great thanks! And I can add and remove to the vector just like any other item in a vector?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @leinad
        Yes.

        And for

        I tried using QVariant but it does not accept QGraphicsEllipseItem *. Is this possible?

        It will only accept the types listed in https://doc.qt.io/qt-5/qvariant.html#public-functions. And as I discovered yesterday, that does not include * anything :( You could probably store that as a qulonglong or similar, with yucky casting, also :(

        fcarneyF 1 Reply Last reply
        1
        • L leinad

          Great thanks! And I can add and remove to the vector just like any other item in a vector?

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #5

          @leinad Yes, you are basically storing two ints and a pointer, so? I assume you take care of a proper item release in the case of a pointer.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • L leinad

            Hi,

            I'm trying to create a QLIst or QVector (whichever is easier) to allow me to have mixed types such as 'int' and QGraphicsEllipseItem.

            I tried using QVariant but it does not accept QGraphicsEllipseItem *. Is this possible? I'd like to iterate through each List index for the specific row and column values and use QGraphicsItem as I need to. Once I have the list it should be easier to iterate but I can add mixed types to the list. I suppose I can have two vectors (one for int's and the other for QGraphicsEllipseItem ) but it's easier to maintain one list if I can do it.

            Below is a example of what I'm looking to do.

            List[0]
            int row
            int column
            QGraphicsEllipseItem *
            List[1]
            int row
            int column
            QGraphicsEllipseItem *
            etc

            Thanks!

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #6

            @leinad

            If you need do to more complex stuff, you can replace the struct, @artwaw suggested, with a class which holds your ints, items and maybe other members or additional functions.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            JonBJ 1 Reply Last reply
            0
            • L Offline
              L Offline
              leinad
              wrote on last edited by
              #7

              Thanks, but it is not that complex, so the structs should probably do it :)

              1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @leinad

                If you need do to more complex stuff, you can replace the struct, @artwaw suggested, with a class which holds your ints, items and maybe other members or additional functions.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #8

                @Pl45m4
                Purely for the record/anyone reading, you can do that just as much in a struct as in a class. Not that I think it's to be recommended, but still.

                Pl45m4P 1 Reply Last reply
                1
                • JonBJ JonB

                  @Pl45m4
                  Purely for the record/anyone reading, you can do that just as much in a struct as in a class. Not that I think it's to be recommended, but still.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #9

                  @JonB said in How to Create a QList or QVector of mixed types:

                  you can do that just as much in a struct as in a class

                  As usual: just because you can, it doesn't mean, you should :-)

                  Using structs as class is weird OOP design (esp. when it comes to inheritance and member vars). AFAIK all struct members are public.


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #10

                    For your case, you could also use a vector of QPair/std::pair, where the vector type is QVector<QPair<QPoint, QGraphicsEllipseItem *>> and the QPoint would contain the column/row values.

                    S 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @leinad
                      Yes.

                      And for

                      I tried using QVariant but it does not accept QGraphicsEllipseItem *. Is this possible?

                      It will only accept the types listed in https://doc.qt.io/qt-5/qvariant.html#public-functions. And as I discovered yesterday, that does not include * anything :( You could probably store that as a qulonglong or similar, with yucky casting, also :(

                      fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #11

                      @JonB said in How to Create a QList or QVector of mixed types:

                      that does not include * anything

                      It will store any QMetaType.
                      https://doc.qt.io/qt-5/qvariant.html#fromValue
                      https://doc.qt.io/qt-5/qvariant.html#setValue

                      You can store QObject* as it is in the meta types list (wherever that is).
                      However, QGraphicsEllipseItem isn't QObject based. I have no idea if it is a meta type.

                      Supposedly you can add to the meta types. But I have not played with that.

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      1
                      • M mchinand

                        For your case, you could also use a vector of QPair/std::pair, where the vector type is QVector<QPair<QPoint, QGraphicsEllipseItem *>> and the QPoint would contain the column/row values.

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on last edited by
                        #12

                        @Pl45m4 said in How to Create a QList or QVector of mixed types:

                        AFAIK all struct members are public.

                        Nope. All struct members are public by default. Just as with a regular class you can declare them private or protected. The only difference between class and struct is the default visibility. And yes, it is bad OOP design, but OOP is not always the best solution. Bjarne Stroutrup himself also talks about data type design. There is no reason to add getters and setters as only member functions if your class is actually just a struct. Don't make things too complicated.

                        @mchinand said in How to Create a QList or QVector of mixed types:

                        For your case, you could also use a vector of QPair/std::pair, where the vector type is QVector<QPair<QPoint, QGraphicsEllipseItem *>> and the QPoint would contain the column/row values.

                        That'll certainly work and sometimes this is the quick way of doing things which I also apply. However, using a struct is a lot more descriptive. I hate it if types are nested too much and you can't figure out what they mean. (Though I'll gladly do the same 😉.)

                        1 Reply Last reply
                        3

                        • Login

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