Results 1 to 4 of 4

Thread: QMap, QHash and QByteArray

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default QMap, QHash and QByteArray

    I'd like to do:

    QHash<QByteArray, some_type> and
    QMap<QByteArray, some_type>

    but can't do either, as there is no hashing function defined for QHash and no comparison function defined for QMap. I'm almost certain there exist quick fixes to this situation, just as they do with STL, please share them with me.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap, QHash and QByteArray

    What Qt version you're using?

    This code

    Qt Code:
    1. #ifndef WIDGET1_H
    2. #define WIDGET1_H
    3.  
    4. #include <QtGui/QWidget>
    5.  
    6. #include <QMap>
    7. #include <QHash>
    8.  
    9. namespace Ui {
    10. class Widget1;
    11. }
    12.  
    13. class Widget1 : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Widget1(QWidget *parent = 0);
    19. ~Widget1();
    20.  
    21. private:
    22. Ui::Widget1 *ui;
    23.  
    24. QMap <QByteArray, int> myMap;
    25. QHash <QByteArray, int> myHash;
    26. };
    27.  
    28. #endif // WIDGET1_H
    To copy to clipboard, switch view to plain text mode 

    compile
    A camel can go 14 days without drink,
    I can't!!!

  3. The following user says thank you to mcosta for this useful post:

    ugluk (7th July 2011)

  4. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMap, QHash and QByteArray

    This works too, with custom datatype:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. struct s_{
    4. QString str;
    5. int val;
    6. };
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QCoreApplication a(argc, argv);
    11.  
    12. s_ s; s.str = "hello"; s.val = 10;
    13. QMap<QByteArray,s_> map;
    14. QHash<QByteArray,s_> hash;
    15. map["string"] = s;
    16. hash["other key"] = s;
    17.  
    18. qDebug() << map["string"].str << map["string"].val;
    19. qDebug() << hash["other key"].str << hash["other key"].val;
    20.  
    21. return 0;
    22. }
    To copy to clipboard, switch view to plain text mode 
    What errors do you get ?

  5. The following user says thank you to stampede for this useful post:

    ugluk (7th July 2011)

  6. #4
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default Re: QMap, QHash and QByteArray

    I've upgraded to a new version and everything seems to work now, thank you for helping. I used to use 4.6.2.

Similar Threads

  1. Serializing QHash to a QByteArray
    By PeterThePuter in forum Qt Programming
    Replies: 3
    Last Post: 17th December 2010, 23:19
  2. using QReadWriteLock in QMap and Qhash
    By HERC in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 13:00
  3. Set QHash and QMap value directly
    By wirasto in forum Qt Programming
    Replies: 2
    Last Post: 17th December 2009, 19:21
  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

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.