Results 1 to 4 of 4

Thread: How to store custom class in QHash?

  1. #1
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy How to store custom class in QHash?

    Hi

    I have written a class User. Its a simple class which stores some information about user. Here is a code:

    user.h
    Qt Code:
    1. #ifndef USER_H
    2. #define USER_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QHostAddress>
    7. #include <QDateTime>
    8. #include <QDataStream>
    9.  
    10. class User : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. User();
    16. User(QString &firstName, QString &lastName);
    17. User(const User &newUser);
    18. ~User();
    19. void setIpAddress(const QHostAddress &newIpAddress);
    20. void setConnectionDateTime(const QDateTime &newConnectionDateTime);
    21. void setFirstName(const QString &newFirstName);
    22. void setLastName(const QString &newLastName);
    23. QString firstName() const {return userFirstName;}
    24. QString lastName() const {return userLastName;}
    25. QHostAddress ipAddress() const {return userIpAddress;}
    26. QDateTime connectionDateTime() const {return userConnectionDateTime;}
    27. quint16 userSize() const;
    28. bool operator==(const User & other) const;
    29. private:
    30. QString userFirstName;
    31. QString userLastName;
    32. QHostAddress userIpAddress;
    33. QDateTime userConnectionDateTime;
    34. };
    35.  
    36. Q_CORE_EXPORT QDataStream & operator<<(QDataStream & out, const User &user);
    37. Q_CORE_EXPORT QDataStream & operator>>(QDataStream & in, User &user);
    38.  
    39. #endif // USER_H
    To copy to clipboard, switch view to plain text mode 

    user.cpp

    Qt Code:
    1. #include "user.h"
    2. #include <QByteArray>
    3. #include <QDataStream>
    4.  
    5. User::User()
    6. :userFirstName(""), userLastName("")
    7. {
    8.  
    9. }
    10.  
    11. User::User(QString &firstName, QString &lastName)
    12. :userFirstName(firstName), userLastName(lastName)
    13. {
    14.  
    15. }
    16.  
    17. User::User(const User &newUser)
    18. {
    19. userFirstName = newUser.firstName();
    20. userLastName = newUser.lastName();
    21. userIpAddress = newUser.ipAddress();
    22. userConnectionDateTime = newUser.connectionDateTime();
    23. }
    24.  
    25. User::~User()
    26. {
    27.  
    28. }
    29.  
    30. void User::setIpAddress(const QHostAddress &newIpAddress)
    31. {
    32. userIpAddress = newIpAddress;
    33. }
    34.  
    35. void User::setConnectionDateTime(const QDateTime &newConnectionDateTime)
    36. {
    37. userConnectionDateTime = newConnectionDateTime;
    38. }
    39.  
    40. void User::setFirstName(const QString &newFirstName)
    41. {
    42. userFirstName = newFirstName;
    43. }
    44.  
    45. void User::setLastName(const QString &newLastName)
    46. {
    47. userLastName = newLastName;
    48. }
    49.  
    50. quint16 User::userSize() const
    51. {
    52. QByteArray *array = new QByteArray;
    53. QDataStream stream(array, QIODevice::WriteOnly);
    54. stream << userFirstName << userLastName << userIpAddress << userConnectionDateTime;
    55. quint16 size = array->size();
    56. return size;
    57. }
    58.  
    59. bool User::operator==(const User & other) const
    60. {
    61. bool state;
    62. if (userFirstName == other.firstName() && userLastName == other.lastName())
    63. state = true;
    64. else
    65. state = false;
    66.  
    67. return state;
    68. }
    69.  
    70. QDataStream &operator<<(QDataStream & out, const User &user)
    71. {
    72. out << user.firstName() << user.lastName() << user.ipAddress() << user.connectionDateTime();
    73. return out;
    74. }
    75.  
    76. QDataStream &operator>>(QDataStream & in, User &user)
    77. {
    78. QString firstName;
    79. QString lastName;
    80. QHostAddress address;
    81. QDateTime dateTime;
    82.  
    83. in >> firstName >> lastName >> address >> dateTime;
    84.  
    85. user.setFirstName(firstName);
    86. user.setLastName(lastName);
    87. user.setIpAddress(address);
    88. user.setConnectionDateTime(dateTime);
    89. return in;
    90. }
    To copy to clipboard, switch view to plain text mode 

    In the other part of my program I am trying to put this class into QHash like this:
    Qt Code:
    1. QHash<User, QTcpSocket *> connections;
    To copy to clipboard, switch view to plain text mode 

    But when I try to compile it i get the error message:

    ../../../Qt_4.3.4/include/QtCore/../../src/corelib/tools/qhash.h instantiated from `bool QHash<Key, T>::contains(const Key&) const [with Key = User, T = QTcpSocket*]'
    ../../../Qt_4.3.4/include/QtCore/../../src/corelib/tools/qhash.h no matching function for call to `qHash(const User&)'

    What shoud I do to make possible to add this class into QHash?

    Thanks.

  2. #2
    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: How to store custom class in QHash?

    QObjects can't be copied and QHash requires the key class to be mutable and have the qHash() function defined. From what I see you violated both requirements

    Your class doesn't seem to benefit from being a QObject subclass and furthermore the serialization mechanism probably won't work with it either. I suggest you remove the QObject legacy and implement qHash for your class.

    If you require your class to be a QObject, then I suggest storing pointers to those objects instead of objects themselves in the hash.

  3. #3
    Join Date
    Jun 2008
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to store custom class in QHash?

    Thanks.

    Ok, I can remove the QObject legacy but how to implement qHash for my class? Can you write a little example?

  4. #4
    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: How to store custom class in QHash?

    As far as I remember there is a nice example in the docs. In general the function should return an integer for any object of your class - always the same integer for the same object content and different integers for different content with an equal spread of values among the object space.

Similar Threads

  1. QListWidget inheriting custom class
    By phannent in forum Qt Tools
    Replies: 1
    Last Post: 4th August 2008, 14:39
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. Store an custom enum in QSettings
    By Lykurg in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 20:06
  4. 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.