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 custom compare functor
Forum Updated to NodeBB v4.3 + New Features

QSet with custom compare functor

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xtech
    wrote on last edited by
    #1

    Hi,

    is there any way to define custom compare functor for QSet? For std::set we can define

    std::set<MemoryLayoutInfo,MemoryLayoutInfo::Compare> set;

    and the same for std::unordered set but in documentation about QSet class I didnt see similar constructor so... this class does not support this feature?

    1 Reply Last reply
    0
    • BjornWB Offline
      BjornWB Offline
      BjornW
      wrote on last edited by BjornW
      #2

      From the docs:

      "QSet's value data type must be an assignable data type. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, the type must provide operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key's type. See the QHash documentation for a list of types supported by qHash()."

      If you look at QHash docs there are examples of how to implement this custom qHash.

      http://doc.qt.io/qt-5/qhash.html

      1 Reply Last reply
      0
      • X Offline
        X Offline
        xtech
        wrote on last edited by
        #3

        I read about qHash but I do not understand how this can help me to reorder items in QSet in custom way? qHash is not the same as compare function.

        For example using std::set I can implement custom comparator like below

        class Compare {
            public:
                bool operator()(const MemoryLayoutInfo &x,const MemoryLayoutInfo &y) {
                    return x.bits < y.bits ? true  :
                           x.bits > y.bits ? false :
                           x.data <= y.data  ? true  : false;
                }
            };
        
        1 Reply Last reply
        0
        • BjornWB Offline
          BjornWB Offline
          BjornW
          wrote on last edited by
          #4

          From the docs:

          "QSet<T> is one of Qt's generic container classes. It stores values in an unspecified order and provides very fast lookup of the values." So no ordering.

          Do you need to use a Qt container? If not, std::set<T> is ordered. (std::unordered_set<T> is unordered)

          I'm unsure what's the best Qt approach for a ordered container. QMap is ordered by key, but that seems like overkill for the job.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            From what you wrote QSet is not the class you want to use since as @BjornW already took from the documentation, it's an unordered container.

            There's no equivalent of std::set in Qt. QSet matches std::unordered_set.

            You can find a good analysis of Qt's containers here.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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