QVariant pointer not respond value
-
Hi!
I have a QVariant pointer:
QVariant *param;
I passing to an integer value:
int p = 12; param = (QVariant*)(&p);
But i do not get the value, i get always 0:
param->toInt();
The gdb write this:
(gdb) print *param $7 = {d = {data = {c = 12 '\f', uc = 12 '\f', s = 12, sc = 12 '\f', us = 12, i = 12, u = 12, l = 12, ul = 12, b = 12, d = 5.1185933671348506e-309, f = 1.68155816e-44, real = 5.1185933671348506e-309, ll = 1036014831271948, ull = 1036014831271948, o = 0xc, ptr = 0xc, shared = 0xc}, type = 0, is_shared = 0, is_null = 0}}
What do I get rid of?
Thank you for answers.
-
@Kutyus said in QVariant pointer not respond value:
param->toInt();
honestly i do not know why your code doesn't crash o.O
You are casting the stack-pointer address ofp
to a QVariant pointer and later you access this invalid pointer.I see what you are trying to do and it should rather look like this:
param = new QVariant(p);
-
Hi,
@Kutyus said in QVariant pointer not respond value:param = (QVariant*)(&p);
instead you do like this, try this
param =QVariant::fromValue(&p); -
@Venkatesh-V
It is wrong, param is QVariant*, not QVariant. -
@Venkatesh-V said in QVariant pointer not respond value:
param =QVariant::fromValue(&p);
this also results in an invalid pointer! DO NOT use stack-pointer addresses for such cases.
@Kutyus said in QVariant pointer not respond value:
I did not write down everything, of course there is a
param = new QVariant();line.
Still you want to use QVariant implicit conversion constructor / assignment operator. So pass the int value not it's pointer address!
-
@raven-worx said in QVariant pointer not respond value:
honestly i do not know why your code doesn't crash o.O
Nothing to crash, but it's pure "luck". ;)