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. Functor. Implementing comparator with additional parameter
Forum Updated to NodeBB v4.3 + New Features

Functor. Implementing comparator with additional parameter

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.8k Views 2 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.
  • M Offline
    M Offline
    Maser
    wrote on last edited by Maser
    #1

    Hi.
    Im trying to implement a comparator to sort my QList<Custom>. Problem ist, the comparator for qSort obv have to be a static function, just taking two arguments. But i need a third argument, so i can do my comparison, or i need somehow to access member variables of my class. Is there a way to do this?
    I came across something called functor, that somehow make a subclass of myclass and then use member variables and implementing "operator()" , but i dont get how to do it and if its even possible with the signal and slot stuff.

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

      Hi,

      Can you show a sample of how you would like to do your comparison ?

      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
      0
      • M Offline
        M Offline
        Maser
        wrote on last edited by Maser
        #3

        Well, i have a QList<Village> containing villages with a name and two coordinates. I have a program that display i number of villages in a TableView. I want to sort the villages with respect to their distance to a specific targetvillage. Which means the sorting changes every time the targetvillage is changed. This means i need an additional argument for the comparison, namely a pointer, reference or whatever for the current targetvillage.

        class Village
        {
        public:
        ...
        
        private:
            QString villageName;
            int x;
            int y;
        };
        

        so far i had something like

        
         void TableDataModel::sort(int column, Qt::SortOrder order)
         {
               qSort(villageList->begin(), villageList->end(), compareTwoVillageLessThan);
         }
        
        bool TableDataModel::compareTwoVillageLessThan(const Village &A, const Village &B )
        {
            Village tempTargetVillage = *targetVillage;
            double distanceA = calculateDistance::getCalculateDistance(A, tempTargetVillage, c);
            double distanceB = calculateDistance::getCalculateDistance(B, tempTargetVillage, c);
            return distanceA < distanceB;
        }
        

        but then i realized the comparator have to be static and i cant access class member.

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

          Use a functor:

          class CompareTwoVillages
          {
          private:
             const Village refVillage;
          
          public:
          
             CompareTwoVillages(const Village &village) : refVillage(village)
             {
             }
          
             bool operator () (const Village& leftVillage, const Village& rightVillage)
             {
                  bool result = false;
                  // implement algorithm
                  return result;
             }
          };
          

          And then call:

          qSort(villageList.begin(), villageList.end(), CompareTwoVillages(targetVillage));
          

          By the way, why do you allocate your QList on the heap ? That's usually not needed.

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

          kshegunovK 1 Reply Last reply
          3
          • SGaistS SGaist

            Use a functor:

            class CompareTwoVillages
            {
            private:
               const Village refVillage;
            
            public:
            
               CompareTwoVillages(const Village &village) : refVillage(village)
               {
               }
            
               bool operator () (const Village& leftVillage, const Village& rightVillage)
               {
                    bool result = false;
                    // implement algorithm
                    return result;
               }
            };
            

            And then call:

            qSort(villageList.begin(), villageList.end(), CompareTwoVillages(targetVillage));
            

            By the way, why do you allocate your QList on the heap ? That's usually not needed.

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @Maser
            I have two tiny remarks to the otherwise excellent guidance provided by @SGaist:

            1. You could use a lambda function (which internally is a functor) instead of writing a whole class. It's just a more compact notation for what Samuel suggested.
            2. You should use std::sort instead of qSort, as qSort is marked obsolete in Qt 5.

            Kind regards.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            2
            • M Offline
              M Offline
              Maser
              wrote on last edited by
              #6

              Thank you very much, guys))
              Your example helped me a lot, SGaist. Although i had to do a bit of reading on functors and function pointers to fully understand the concept.
              I let it sink in a bit, and then im gonna read some more on lambdafunctions as kshegunov suggested.
              Thubms up!

              1 Reply Last reply
              0

              • Login

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