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

Qt Code:
  1. //Interface
  2. virtual void* get_Param(char* str)
To copy to clipboard, switch view to plain text mode 

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.

Qt Code:
  1. myclass.h
  2. QHash<QString,QVariant> listParam;
To copy to clipboard, switch view to plain text mode 

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

Qt Code:
  1. void* ImageFileSource::getParam(char* paramStr){
  2. QString s =QString::fromStdString(std::string(paramStr));
  3. if (listParam.contains(s))
  4. {
  5. return (void*)(&(listParam.value(s)));
  6. }
  7. else
  8. return NULL;
  9. }
To copy to clipboard, switch view to plain text mode 

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,

Qt Code:
  1. //main.cpp -Working
  2. int h;
  3. bool ok;
  4. q=(QVariant*)cam.getParam("Height");
  5. h=q->toInt(&ok);
  6. cout << h << endl;
To copy to clipboard, switch view to plain text mode 

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

Qt Code:
  1. //main.cpp -Not Working
  2. int h,w;
  3. bool ok;
  4. q=(QVariant*)cam.getParam("Height");
  5. h=q->toInt(&ok);
  6. w=q->toInt(&ok);
  7. cout << q->toString << endl;
To copy to clipboard, switch view to plain text mode 

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.

Qt Code:
  1. //main.cpp -Not Working
  2. int h,w;
  3. bool ok;
  4. q=(QVariant*)cam.getParam("Height");
  5. q2=(QVariant*)cam.getParam("Width");
  6. h=q->toInt(&ok);
  7. w=q2->toInt(&ok);
To copy to clipboard, switch view to plain text mode 

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!!