Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:43:32+00:00 2026-06-14T03:43:32+00:00

I am using QT 4.8 and I notice that it has a QHash class

  • 0

I am using QT 4.8 and I notice that it has a QHash class which can be used as follows:

  QHash<QString, int> hash;
  hash["one"] = 1;
  hash["three"] = 3;
  hash["seven"] = 7;
  hash.insert("twelve", 12);

If there is a hash collision, will it be handled correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:43:33+00:00Added an answer on June 14, 2026 at 3:43 am

    Yes, collisions will be handled. QHash is a standard implementation of the classic hash-table based container and wouldn’t be very reliable if it didn’t handle collisions correctly. Typically a hash-table based container will map keys not to a single entry in the list but to a “bucket” which may contain more than one entry where different keys map to the same hash value.

    When fetching values, the hash value for the key leads to the correct bucket then the container will iterate through the entries in the bucket until it finds a match for the particular key you are looking for.

    Although I could not find a specific reference in the documentation to the “correctness” of Qt’s implementation, this quote eludes to it. I can’t imagine it being otherwise.

    QHash’s internal hash table grows by powers of two, and each time it
    grows, the items are relocated in a new bucket, computed as qHash(key)
    % QHash::capacity() (the number of buckets).

    A simple test will increase our confidence:

    BadHashOjbect.h

    #ifndef BADHASHOBJECT_H
    #define BADHASHOBJECT_H
    
    class BadHashObject
    {
    public:
        BadHashObject(const int value): value(value){}
    
        int getValue() const
        {
            return value;
        }
    
    private:
        int value;
    };
    
    bool operator==(const BadHashObject &b1, const BadHashObject &b2)
    {
        return b1.getValue() == b2.getValue();
    }
    
    uint qHash(const BadHashObject &/*key*/)
    {
        return 1;
    }
    
    #endif // BADHASHOBJECT_H
    

    main.cpp

    #include <iostream>
    #include <QHash>
    #include "BadHashObject.h"
    
    using namespace std;
    
    int main(int , char **)
    {
        cout << "Hash of BadHashObject(10) is: " << qHash(BadHashObject(10)) << endl;
        cout << "Hash of BadHashObject(100) is: " << qHash(BadHashObject(100)) << endl;
        cout << "Adding BadHashObject(10), value10 and BadHashObject(100), value100" << endl;
        QHash<BadHashObject, QString> hashMap;
        hashMap.insert(BadHashObject(10), QString("value10"));
        hashMap.insert(BadHashObject(100), QString("value100"));
        cout << "Size of hashMap: " << hashMap.size() << endl;
        cout << "Value stored with key 10: " << hashMap.value(BadHashObject(10)).toStdString() << endl;
        cout << "Value stored with key 100: " << hashMap.value(BadHashObject(100)).toStdString() << endl;
    }
    

    The BadHashObject class stores an int and its hash function will always return 1 so all objects added to a QHash using this type as a key will result in a collision. The output from our test program shows that the collision is handled properly.

    Hash of BadHashObject(10) is: 1
    Hash of BadHashObject(100) is: 1
    Adding BadHashObject(10), value10 and BadHashObject(100), value100
    Size of hashMap: 2
    Value stored with key 10: value10
    Value stored with key 100: value100
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I hide the scrollbar of using css/jQuery? I notice that the slideDown()
I notice that when using sys.getsizeof() to check the size of list and dictionary,
I notice that many of the WPF MVVM frameworks seem to avoid using the
I'm using Drupal and I notice that if I have my CSS files aggregated
when using the module pattern in js i've notice that the main benefits are
I am using PHP mail() to send daily notice emails. But I noticed that
I just started using Qt and noticed that all the example class definitions have
This is the first time that this error has come up. I am using
I'm using a small tree/graph package ( django_dag ) that has that give my
I am using spy++ and see that the control I have has the decimal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.