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. Qt6 QString[] QChar compare with char
Forum Updated to NodeBB v4.3 + New Features

Qt6 QString[] QChar compare with char

Scheduled Pinned Locked Moved Solved General and Desktop
qt6.0.2
8 Posts 4 Posters 2.5k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on last edited by
    #1

    Hey
    Say I have something like this...

    QString te("dcfsacsa")
    te[0] == "#"
    

    What is the proper way of doing it under Qt6 as atm it just errors out with

    error C2678: binary '==': no operator found which takes a left-hand operand of type 'QChar' (or there is no acceptable conversion)
    
    JonBJ 1 Reply Last reply
    0
    • D Dariusz

      Hey
      Say I have something like this...

      QString te("dcfsacsa")
      te[0] == "#"
      

      What is the proper way of doing it under Qt6 as atm it just errors out with

      error C2678: binary '==': no operator found which takes a left-hand operand of type 'QChar' (or there is no acceptable conversion)
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @Dariusz said in Qt6 QString[] QChar compare with char:

      te[0] == "#"

      I know nothing about Qt6, or whether there is something special there. However, I think you have mistyped. te[0] is QChar, but you are comparing against "#", which is a (literal) string, not a char. I think you intended

      if (te[0] == '#') ...
      
      1 Reply Last reply
      5
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #2

        That isn't a comparison between QString and char, but rather QChar and char. Convert the right argument to a QChar.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • D Dariusz

          Hey
          Say I have something like this...

          QString te("dcfsacsa")
          te[0] == "#"
          

          What is the proper way of doing it under Qt6 as atm it just errors out with

          error C2678: binary '==': no operator found which takes a left-hand operand of type 'QChar' (or there is no acceptable conversion)
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Dariusz said in Qt6 QString[] QChar compare with char:

          te[0] == "#"

          I know nothing about Qt6, or whether there is something special there. However, I think you have mistyped. te[0] is QChar, but you are comparing against "#", which is a (literal) string, not a char. I think you intended

          if (te[0] == '#') ...
          
          1 Reply Last reply
          5
          • Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by Ketan__Patel__0011
            #4

            if you want to get singal character from your string
            then you can use QString::at() it return the character from your string at particular index.

            Try This

            QString te("dcfsacsa");
            
            if(te.at(0) == '#')
            {
            /// Your Code
            }
            else
            {
            /// Your Code
            }
            
            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dariusz
              wrote on last edited by
              #5

              Hey

              Thanks! The QString::at() looks better than the QChar("#") I was thinking of doing. Should be faster I take?

              Regards
              Dariusz

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Dariusz
                wrote on last edited by
                #6

                Ok so QString::at() returns QChar. So that is not help

                Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?

                I end up with QString[0] == QString("#")

                I'm a bit worried with overhead here...

                JonBJ 1 Reply Last reply
                0
                • D Dariusz

                  Ok so QString::at() returns QChar. So that is not help

                  Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?

                  I end up with QString[0] == QString("#")

                  I'm a bit worried with overhead here...

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Dariusz

                  Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?

                  Did you read what I wrote above? I told you why your "#" won't work. It's a good idea to read and act on answers rather than ignoring them....

                  D 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Dariusz

                    Also I cant do QString[0] == QChar("#") because there is apparently no constructor for it?

                    Did you read what I wrote above? I told you why your "#" won't work. It's a good idea to read and act on answers rather than ignoring them....

                    D Offline
                    D Offline
                    Dariusz
                    wrote on last edited by
                    #8

                    @JonB Oh buggers... I missed the " to ' change. Thanks! Yea that make more sense now o.o

                    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