How to Create a QList or QVector of mixed types
-
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 *
etcThanks!
-
-
@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 aqulonglong
or similar, with yucky casting, also :( -
@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
. -
@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#setValueYou 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.
-
@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 themprivate
orprotected
. The only difference betweenclass
andstruct
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 😉.)