QVector filled with pointers return hex value
-
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
like
@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:
@
int a;
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
@
QVariant a(2);
QVector<QVariant*> data;
data.append(&a);
a = 10;
qDebug() << data.last()->toInt(); //prints 10
a = QStringLiteral("foo");
qDebug() << data.last()->toString(); //prints "foo"
@ -
Hi,
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
-
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
@
cont.append(&QVariant::fromValue<int*>(&a));
@A pointer to QVariant that holds a pointer to int? Why not just store pointer to int then?
also this
@
QVariant::fromValue<int*>(&a)
@
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.
@QString finalstring;
for(int i = 0; i < cont.count(); i++)
{
finalstring += QString("%1 %2").arg(cont1[i]).arg(cont[i]);
}emit ...(finalstring);@
cont1 mean vector with string values
cont mean vector with data typessimple function will add to containers
@void add(QString text, QVariant data)
{
cont1.append(text);
cont.append(..........);
}@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.