Results 1 to 7 of 7

Thread: Basic hash problem

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Basic hash problem

    Hi,
    I declared a variable h of type QHash<const char *,const char *>.
    I called h.insert("foo","bar")
    then I test to see if h.contains("foo") returns true. It does. However I have another case where
    I use a variable that points to a string s identical to foo in content and i call
    h.insert(s,"baz")
    this time h.contains("foo") returns false.
    Any suggestions?

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Basic hash problem

    Quote Originally Posted by feraudyh View Post
    Hi,
    I declared a variable h of type QHash<const char *,const char *>.
    I called h.insert("foo","bar")
    then I test to see if h.contains("foo") returns true. It does. However I have another case where
    I use a variable that points to a string s identical to foo in content and i call
    h.insert(s,"baz")
    this time h.contains("foo") returns false.
    Any suggestions?
    You need to insert the string after converting it to char*.
    Like this.
    Qt Code:
    1. h.insert(s.toUtf8().data(), "baz");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic hash problem

    Thankyou Yogeshgokul,
    I should have said that s is of type char *, and when I use the following program to print the keys they are as expected
    QList<const char *> config_keys = config->keys();
    int len = config_keys.count();
    printf("len = %d\n",len);
    for(int i=0; i < len; i++)
    {
    printf("key:%s\n",config_keys[i]);
    }
    In the above config is a pointer to my hash table.
    However

    if (!(config->contains("port")))
    {
    printf( "port is not a key\n");
    }
    prints "port is not a key", despite the fact it appears in my previous display of the keys.

  4. #4
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic hash problem

    OK I have somplifed my code to this which is surprising to me:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QHash>
    3. #include <iostream>
    4.  
    5.  
    6. QHash<const char*,const char*> *config;
    7. int main(int argc, char *argv[])
    8. {
    9. using std::cout;
    10.  
    11. QCoreApplication a(argc, argv);
    12. char *key1, *val1;
    13. config = new QHash<const char *,const char *>;
    14. key1 = new char[5];
    15. key1[0] = 'p';
    16. key1[1] = 'o';
    17. key1[2] = 'r';
    18. key1[3] = 't';
    19. key1[4] = '\0';
    20. val1 = new char[2];
    21. val1[0] = '5';
    22. val1[1] = '\0';
    23. config->insert(key1,val1);
    24. if(!(config->contains("port")))
    25. cout<<"port is not a key";
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    When run it prints
    port is not a key.

    Now of course there is something a bit fishy in my code: i am mixing char * and const char *. I would never have expected expected it could lead to this!!
    I should point out I was a C programmer a long time ago and I am not used to the const
    word.
    Last edited by feraudyh; 1st December 2009 at 15:55.

  5. #5
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Basic hash problem

    I have just edited and simplified the previous post.

    I'd like to point out that I just removed the word const from the above code and the problem is still there!!

    Well, I'll just work around the problem by using QString for the first type instead of
    char *.
    Last edited by feraudyh; 1st December 2009 at 18:57.

  6. #6
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Basic hash problem

    Hi, it looks like the task you try to solve already done by QSettings. Please, have a look in assistant for it.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Basic hash problem

    You have a hash that has a pointer to a const char as a key. If you have two character strings containing the same text (like "port" in your case) the pointers will be different, meaning that this returns false (check it out if you want):
    Qt Code:
    1. const char *p1 = "port";
    2. const char *p2 = "port";
    3. if(p1==p2)
    4. printf("the same\n");
    5. else
    6. printf("different\n");
    To copy to clipboard, switch view to plain text mode 

    If you use C++ then avoid using char* for strings as they have no semantics. Use std::string or QString (if you use Qt - and you do) instead.
    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. Basic: problem building designer application
    By kingslee in forum Qt Tools
    Replies: 2
    Last Post: 31st August 2006, 15:26
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.