Results 1 to 7 of 7

Thread: QHash initializing

  1. #1

    Default QHash initializing

    Hi all,

    Is it mandatory to initialize QHash before insert ? I have a crash on insert, althought i do insert non NULL values with the right types. My hash is a data member.

    Qt Code:
    1. QHash<QListWidgetItem* , QStringList> m_variablepaths ;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,491
    Thanks
    252
    Thanked 717 Times in 707 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QHash initializing

    Except for the copy constructor there is no such thing as "initializing". You're apparently doing something wrong when inserting, but without any code it is impossible to know.

  3. #3
    Join Date
    Jul 2012
    Posts
    228
    Thanks
    19
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash initializing

    also, if you're using a pointer as a key, a QHash may not be the best choice.

  4. #4

    Default Re: QHash initializing

    It falls in Qt method detach(). Is that of any help for guiding me towards solution ?

    Is it important that I pass a pointer to QstringList, and not a stack QStringList ?

    What is better then Qhash ? why is Qhash not a good choice ? Should I use QMap instead ?

    I tried to initialize in constructor, and i tried with a QMap, with same result.
    Last edited by trust88; 3rd May 2013 at 18:16.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,491
    Thanks
    252
    Thanked 717 Times in 707 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QHash initializing

    It is irrelevant what you pass as the "value" part of the hash. If you pass a QStringList, the hash table will make a copy of it if needed.

    It falls in Qt method detach(). Is that of any help for guiding me towards solution ?
    Look, throwing out random tidbits of information isn't helping anyone help you. Unless you post some code that actually demonstrates what you are doing, we're just pulling things out of thin air.

    Given some of the other questions you've posted, the errors evident in that code, and the "hint" you've given here, my guess would be that the problem is that something you did prior to inserting values into the hash table has corrupted memory, and that the crash you are seeing is a side effect and not the actual problem.

    Inserting a pointer into a hash table would in no way result in a call to detach() for anything, but if memory is corrupt, the code might be jumping off into some unknown place in your program and executing whatever it finds there.

    What is better then ? why is it not a good choice ?
    If all you need is to associate a QStringList with a QListWidgetItem, then you could use the QListWidgetItem::data() and QListWidgetItem::setData() methods instead of building an external data structure to keep the association.

    Otherwise, QHash<> is fine for this purpose, QMap<> would work too but it is less efficient since it must keep its keys sorted.
    Last edited by d_stranz; 3rd May 2013 at 18:13.

  6. #6
    Join Date
    May 2013
    Posts
    5
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QHash initializing

    I have the same problem posted here, but nobody seems to answer it.

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

    Default Re: QHash initializing

    Quote Originally Posted by trust88 View Post
    It falls in Qt method detach(). Is that of any help for guiding me towards solution ?
    Do you have more than one thread in your application that tries to access this data structure?
    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. initializing QPainter with a QImage
    By Aayush in forum Qt Programming
    Replies: 3
    Last Post: 20th October 2011, 12:47
  2. Loading/Initializing classes
    By alexandernst in forum Qt Programming
    Replies: 6
    Last Post: 6th December 2010, 12:06
  3. Delay initializing?
    By MorrisLiang in forum Newbie
    Replies: 4
    Last Post: 23rd August 2010, 15:37
  4. Initializing a QList of QHashes
    By Cruz in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 16:24
  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.