Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Why does no operator< exist for two QStrings?

    General and Desktop
    3
    5
    808
    Loading More Posts
    • 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.
    • A
      Asperamanca last edited by Asperamanca

      Background: I get a compile error in the following code line:

      return m_Id.operator< (other.m_Id);
      

      Both m_Id and other.m_Id are of type QString.
      Compiler output (MinGW) is:

      error C2664: 'bool QString::operator <(const QByteArray &) const': cannot convert argument 1 from 'const QString' to 'QLatin1String'
      

      That made me scratch my head. Looking up help, I realized that only the following operator< overloads are defined for QString:

      bool operator<(QLatin1String other) const
      bool operator<(const char *other) const
      bool operator<(const QByteArray &other) const
      

      ...as well as this global function:

      bool operator<(const char *s1, const QString &s2)
      

      Why is that? And what's the best way to fix it?

      mrjj Taz742 2 Replies Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion @Asperamanca last edited by

        Hi
        I cant guess reason for the design choices but others have discussed it
        http://stackoverflow.com/questions/16281014/operator-for-qstring

        A 1 Reply Last reply Reply Quote 0
        • Taz742
          Taz742 @Asperamanca last edited by

          @Asperamanca
          i dont understand.
          you need to sort Arraylist or Vector?

          bool comperator(QString a, QString b){
          return a < b;
          }

          Qsort(array.begin(),array.end(),comperator);

          Do what you want.

          A 1 Reply Last reply Reply Quote 0
          • A
            Asperamanca @mrjj last edited by

            @mrjj said in Why does no operator< exist for two QStrings?:

            Hi
            I cant guess reason for the design choices but others have discussed it
            http://stackoverflow.com/questions/16281014/operator-for-qstring

            I think you are sitting on a different boat...I was referring to operator< (less than)

            1 Reply Last reply Reply Quote 0
            • A
              Asperamanca @Taz742 last edited by

              @Taz742 said in Why does no operator< exist for two QStrings?:

              @Asperamanca
              i dont understand.
              you need to sort Arraylist or Vector?

              bool comperator(QString a, QString b){
              return a < b;
              }

              Qsort(array.begin(),array.end(),comperator);

              That gave me the hint. The code it not from me, and I didn't see the obvious: By writing

              return m_Id.operator< (other.m_Id);
              

              instead of

              return m_Id < other.m_Id;
              

              the code explicitly calls one of the overloads in QString, instead of any globally defined function.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post