Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSet with a Custom Class
Forum Updated to NodeBB v4.3 + New Features

QSet with a Custom Class

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MDCatoM Offline
    MDCatoM Offline
    MDCato
    wrote on last edited by
    #1

    I was looking for a very simple example of a Custom Class being used in a QSet, and I could not find one anywhere.

    I have eventually worked out this example, and I wanted to share with everyone, and also get feedback in case I did something wrong.

    myclass.h

    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    #include <QDebug>
    
    class MyClass
    {
    public:
        MyClass();
        MyClass(const MyClass &other);
        MyClass &operator=(const MyClass &other);
        bool operator==(const MyClass &other) const;
    
        QString alpha;
        QString beta;
    };
    
    inline uint qHash(const MyClass &key, uint seed){
        return qHash(key.alpha, seed) ^ qHash(key.beta, seed+1);
    }
    
    QDebug operator<<(QDebug dbg, const MyClass &data);
    #endif // MYCLASS_H
    

    myclass.cpp

    #include "myclass.h"
    
    MyClass::MyClass(){
    
    }
    
    MyClass::MyClass(const MyClass &other){
        alpha = other.alpha;
        beta = other.beta;
    }
    
    MyClass& MyClass::operator=(const MyClass &other){
        MyClass *ret = new MyClass();
        ret->alpha = other.alpha;
        ret->beta = other.beta;
        return *ret;
    }
    
    bool MyClass::operator==(const MyClass &other) const{
        return alpha.compare(other.alpha) == 0 && beta.compare(other.beta) == 0;
    }
    
    QDebug operator<<(QDebug dbg, const MyClass &data){
        dbg.nospace() << "MyClass(" << data.alpha << ", " << data.beta << ")";
        return dbg.maybeSpace();
    }
    

    main.cpp

    #include <QCoreApplication>
    #include <QTimer>
    #include <QDebug>
    
    #include "myclass.h"
    
    int main(int argc, char *argv[]){
        QCoreApplication app(argc, argv);
    
        MyClass a;
        MyClass b;
    
        a.alpha = QString("Hello");
        a.beta = QString("World");
        b.alpha = QString("foo");
        b.beta = QString("bar");
    
        QSet<MyClass> set;
        set.insert(a);
        set.insert(b);
    
        qDebug() << set;
    
        QTimer::singleShot(250, &app, &QCoreApplication::quit);
        return app.exec();
    }
    

    output looks like this:

    QSet(MyClass("foo", "bar"), MyClass("Hello", "World"))
    Press <RETURN> to close this window...
    
    1 Reply Last reply
    4
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Thanks sharing this example. We appreciate your effort. It helps others as well. Enjoy Qt Programming.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved