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. QString in switch statement
Qt 6.11 is out! See what's new in the release blog

QString in switch statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 19.4k 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.
  • ankit thakarA Offline
    ankit thakarA Offline
    ankit thakar
    wrote on last edited by
    #1

    Can't we use QString for comparision in switch statement like below,

    QString str = "TEMP";

    switch (str)
    {
    

    I tried to use like this but it didnot work.

    kshegunovK beeckscheB 2 Replies Last reply
    0
    • ankit thakarA ankit thakar

      Can't we use QString for comparision in switch statement like below,

      QString str = "TEMP";

      switch (str)
      {
      

      I tried to use like this but it didnot work.

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

      @ankit-thakar
      No, C++ doesn't allow for non-integral types in switch statements.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4
      • ankit thakarA ankit thakar

        Can't we use QString for comparision in switch statement like below,

        QString str = "TEMP";

        switch (str)
        {
        

        I tried to use like this but it didnot work.

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @ankit-thakar

        When you want to use a String in a switch statement, you obviosily know which String can be entered. In this case you can use enums.

        enum Strings { String1, String2, etc. }
        

        With this enums you're allowed to use the switch statement. Like

        Strings myString;
        
        switch (myString)
        {
         case Strings::String1:
          break;
         case Strings::String2:
          break;
         ...
         default:
          break;
        }
        

        So you can use the switch and it is readable in your code.

        kshegunovK 1 Reply Last reply
        -1
        • beeckscheB beecksche

          @ankit-thakar

          When you want to use a String in a switch statement, you obviosily know which String can be entered. In this case you can use enums.

          enum Strings { String1, String2, etc. }
          

          With this enums you're allowed to use the switch statement. Like

          Strings myString;
          
          switch (myString)
          {
           case Strings::String1:
            break;
           case Strings::String2:
            break;
           ...
           default:
            break;
          }
          

          So you can use the switch and it is readable in your code.

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

          @beecksche
          The inefficient part would be converting the string you have to a corresponding enum, and this pretty much defeats the whole purpose of having a duplicating enum and a switch construct.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            Just use

            if (str == "...")
            {
               // ...
            }
            else if (str == "...")
            {
               // ...
            }
            //...
            else 
            {
                // ...
            }
            
            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