QVector filled with pointers return hex value
I have little problem.
I'm trying to store pointers in vector, but when I call vector, it returns hex values like 0xf2f744
@ QVector<QVariant*> cont;
int ex = 2; cont.push_back(reinterpret_cast<QVariant*>(&ex)); ex = 10; qDebug() << cont.last();@
Thanks for help
What do you expect to see?
You are printing the pointer's stored address.
I expect to see 10, but I see as you said stored adress.
This code is horrible. There's basically something wrong in every line.
- QVariant is "implicitly shared":http://qt-project.org/doc/qt-5.0/qtcore/implicit-sharing.html class so there's little point in storing it by pointers instead of just storing by value. Also there's a QVariantList class which is usually what you actually want.
- why do you create an int value just to horribly miss-use it in the next line with reinterpret_cast? Just do cont.append(2)
- this reinterpret_cast is wrong! Don't do that. reinterpret_cast is usually a sign that "you're doing it wrong"™ and you are in this case. QVariant just acts like a union, it's not one. You're probably reinterpreting the internal d-pointer and that just begs for horrible crashes at some point
- if you want to modify a value stored in a vector state it clearly: cont[whatever]=
- if you want to get an int back from a QVariant there's a toInt() member on it. qDebug() << QVariant(2) will not print "2", it will print "QVariant(int, 2)"
It was just example, without definition its useless
I have created ten data types which I want to store in this list.
So when I use constant val, it's useless, these data types will change every sec , and for my function where I don't know how many data types exists to add to this container.
Do you understand me now? Container will just ref to data type
Sorry, I don't really get it. You're using QVariant anyway. What is the additional indirection through a pointer for? You can store any data type in QVariant after registering it with qRegisterMetaType.
foreach(QVariant v, data)
case QMetaType::Int: qDebug() << v.toInt(); break;
What's wrong with that?
Or better yet - if those types are related use a common interface and polymorphic dispatch and don't bother with QVariant.
But if you really really want to store pointers here's how:
int a = 2;
if(data.last()->type() == QMetaType::Int)
qDebug() << data.last()->toInt();
//remember to delete
But that's as bad and ugly as it looks.
Hm, but a isn't pointer in QVector
because if I change "a" after append for ex.: to 10 it will show up 2. That's what I need, when I add it to QVector and then modify data type, it will show in QVector
@int a = 2;
int* b = &a;
a = 10;
qDebug() << *b; // show 10@
But in QVector
Well ok, but C++ is a type-safe language so you can't have a pointer of type int* outside of the vector and of type QVariant* inside it, have them point to the same thing and still expect it to be writable by both of them safely.
Essentially (omitting the vector) what you want is this:
double* x = reinterpret_cast<double*>(&a);
string* y = reinterpret_cast<string*>(&a);
*x = 1.3;
*y = "foo";
It makes no sense.
Packing it into a QVariant is also not a solution, because QVariant doesn't hold a reference to a value, it makes a copy of that value. To make it work like you want, you need to ditch the original type and stick to the QVariant. This way it's the same type inside and outside of the vector, but you can still put values of different types in it
a = 10;
qDebug() << data.last()->toInt(); //prints 10
a = QStringLiteral("foo");
qDebug() << data.last()->toString(); //prints "foo"
Last code sounds good to me. Thanks
Just last problem, I cannot use QVariant as default data type, when for example I get from GLWidget mouse position its in QVector2D not in QVariant, so if you know some good convertation from defualt data types to QVariant.
You have an example for that in "QVariant's documentation":http://qt-project.org/doc/qt-4.8/qvariant.html#a-note-on-gui-types It also applies to other types.
I'm currently trying:
@ QVector<QVariant*> cont;
int a = 2; cont.append(&QVariant::fromValue<int*>(&a)); a = 10; qDebug() << cont.last()->value<int*>(); // return 0x6cfc64@
And again, it's example, not final using! I'm just testing it.
There must be something I missing all time.
Again, you are printing the address not the content
When I use:
@qDebug() << cont.last()->toInt();@
Then it print 0, or it's again incorrect?
You're so lucky it prints 0 and doesn't crash.
You're making the same mistake as before, except worse ;)
First of all this
A pointer to QVariant that holds a pointer to int? Why not just store pointer to int then?
creates a temporary QVariant variable, that is destroyed as soon as you hit ; in that line. You're taking an address of that temporary and then you try to de-reference it with cont.last()-> . This pointer is already invalid at that point. It is invalid as soon as append() returns.
What you keep getting wrong is what I said earlier - QVariant does not store a reference to another object. It holds a value itself. You can't modify another object by it's copy held in QVariant. It is also a bad idea to store pointer value(an address) in a QVariant. I mean "you can":http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant but it's just not what it's for.
I keep having a feeling that you want to do something horrible with all that. What are you doing exactly? There definitely is a simpler solution to your problems.
OK, so what I'm doing?
Making function which will add to container data type with value
All from container will be printed into one string.
for(int i = 0; i < cont.count(); i++)
finalstring += QString("%1 %2").arg(cont1[i]).arg(cont[i]);
cont1 mean vector with string values
cont mean vector with data types
simple function will add to containers
@void add(QString text, QVariant data)
and I will just hit add function. I don't know how many times I will use it, that's why it cannot be constant.