Results 1 to 4 of 4

Thread: QtVariant pointer from QHash list

  1. #1
    Join Date
    Aug 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Question QtVariant pointer from QHash list

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtVariant pointer from QHash list

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2011
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QtVariant pointer from QHash list

    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



    Quote Originally Posted by wysota View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QtVariant pointer from QHash list

    Quote Originally Posted by rmat View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2011, 17:33
  2. Replies: 1
    Last Post: 4th December 2010, 17:20
  3. List View with sections for alphabetialy sorted list
    By woodtluk in forum Qt Programming
    Replies: 4
    Last Post: 12th October 2010, 11:50
  4. Replies: 1
    Last Post: 10th February 2009, 09:42
  5. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.