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.
The problem is that you cannot overload
operator==like this: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:This operator will never be called when
QSettries to fiend duplicates because it needs a left argument of typeXand a right of typeX*I can see two possible solutions to this problem:
QSet<X>). This will allow you to overload the correct operators. This solution, however, is not always feasible.QSetwithout needing to overload any operators nor theqHashfunction.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 aQMapwhich maps from id toX*. When you create a new object, do something like this: