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)
//Interface
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.
myclass.h
QHash<QString,QVariant> listParam;
myclass.h
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
void* ImageFileSource::getParam(char* paramStr){
if (listParam.contains(s))
{
return (void*)(&(listParam.value(s)));
}
else
return NULL;
}
void* ImageFileSource::getParam(char* paramStr){
QString s =QString::fromStdString(std::string(paramStr));
if (listParam.contains(s))
{
return (void*)(&(listParam.value(s)));
}
else
return NULL;
}
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,
//main.cpp -Working
int h;
bool ok;
h=q->toInt(&ok);
cout << h << endl;
//main.cpp -Working
int h;
bool ok;
QVariant *q;
q=(QVariant*)cam.getParam("Height");
h=q->toInt(&ok);
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.
//main.cpp -Not Working
int h,w;
bool ok;
h=q->toInt(&ok);
w=q->toInt(&ok);
cout << q->toString << endl;
//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;
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.
//main.cpp -Not Working
int h,w;
bool ok;
h=q->toInt(&ok);
w=q2->toInt(&ok);
//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);
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!!
Bookmarks