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

  • Home
  • SEARCH
  • 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 1017911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:46:39+00:00 2026-05-16T10:46:39+00:00

For a class X and a QSet< X* >, how is it possible to

  • 0

For a class X and a QSet< X* >, how is it possible to make sure that the QSet doesn’t contain duplicate elements?
The unique property in each object of type X is a QString that can be fetched using getName().

I’ve implemented the qHash(X*) function, the operator==(), operator<() and operator>(), but the QSet still accepts duplicate elements, i.e., those with the same Name.

Could someone help me out in making this work?


Ok. Here’s what I’m trying to do.
I have a class Y and a class X, both of which inherit QDialog. A function in class Y ( a slot), is responsible for spawning objects of class X. The dialog for Y is to be made responsible for the X objects spawned. This is why I created a QSet< X* > member in Y.

  • 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-05-16T10:46:40+00:00Added an answer on May 16, 2026 at 10:46 am

    The problem is that you cannot overload operator== like this:

    bool operator==(X*, X*);
    

    This is because at least one of the argument must be of class type.

    Since you say you implemented operator==, I suppose you did something like this:

    struct X
    {
        bool operator==(X*) const;
    };
    

    This operator will never be called when QSet tries to fiend duplicates because it needs a left argument of type X and a right of type X*

    I can see two possible solutions to this problem:

    • Do not store your items as pointers (ie using QSet<X>). This will allow you to overload the correct operators. This solution, however, is not always feasible.
    • If you could enforce somehow that there is only one object with a given id, you could just store pointers in you QSet without needing to overload any operators nor the qHash function.

    Edit: If your design allows to create multiple X-objects with the same id but you only want one such object to exist at any time, maybe it’s best to use a QMap which maps from id to X*. When you create a new object, do something like this:

    QString newId = ...;
    delete objectsMap[newId];
    objectsMap[newId] = new X(newId);
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.