PDA

View Full Version : QtVariant pointer from QHash list



rmat
8th August 2011, 16:55
Hi!

I'm working with Qt and I have a problem with QVariant and QHash. I am using an Interface in order to define a function that should be common for several objects that implements this interface. I return a void* in order to remain as general as possible



//Interface
virtual void* get_Param(char* str)


In my class I use a QHash list that contains all the parameters I need. I defined the QHash as a QVariant list because I want to return both interger or string, depending on the requested parameter.



myclass.h
QHash<QString,QVariant> listParam;


In the implementation of the "getParam" function I cast to (void*) the QVariant* to the desired position



void* ImageFileSource::getParam(char* paramStr){
QString s =QString::fromStdString(std::string(paramStr));
if (listParam.contains(s))
{
return (void*)(&(listParam.value(s)));
}
else
return NULL;
}


The problem comes from the main program, where I call that function. If I call the function and I get the request value once it works,



//main.cpp -Working
int h;
bool ok;
QVariant *q;
q=(QVariant*)cam.getParam("Height");
h=q->toInt(&ok);
cout << h << endl;


But if I try to access twice or more the vaue, only h receive the correct value.



//main.cpp -Not Working
int h,w;
bool ok;
QVariant *q;
q=(QVariant*)cam.getParam("Height");
h=q->toInt(&ok);
w=q->toInt(&ok);
cout << q->toString << endl;


Or if I try to get two pointers to different values, also only "h" receive a correct value. The other values are 0 or null, and the boolean "ok" is 0.



//main.cpp -Not Working
int h,w;
bool ok;
q=(QVariant*)cam.getParam("Height");
q2=(QVariant*)cam.getParam("Width");
h=q->toInt(&ok);
w=q2->toInt(&ok);


Anyone knows why it is happening? I am pretty sure the error comes from my site but I do not understand why. QHash is not keeping the pointer to the same position after using it? If I'm using Parallel programming and several Threads are executed I have to force the pointer call and the conversion of the value together?

Thanks a lot!!

wysota
9th August 2011, 07:06
Returning void* in C++ is a bad idea. Return QVariant instead. Since QVariant also includes QVariantHash, you'll be able to return every type you need. And you can register custom types with QVariant as well.

rmat
9th August 2011, 10:24
Thanks a lot for your answer, I have a question. I try to keep the Interface class as general as possible. I void like to be able of sending a class. Should I use then QObject? And if I want be also independent of any library what I could use instead of void*? (I'm using void* also in other functions, where I return an object)

Thanks a lot again




Returning void* in C++ is a bad idea. Return QVariant instead. Since QVariant also includes QVariantHash, you'll be able to return every type you need. And you can register custom types with QVariant as well.

wysota
9th August 2011, 10:28
Thanks a lot for your answer, I have a question. I try to keep the Interface class as general as possible. I void like to be able of sending a class. Should I use then QObject?
QVariant also accepts pointers to QObject instances.


And if I want be also independent of any library what I could use instead of void*? (I'm using void* also in other functions, where I return an object)
If you don't want to rely on Qt then create your own variant class with strict conversion rules and use that.