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

QString in switch statement

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 17.9k 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.
  • A Offline
    A Offline
    ankit thakar
    wrote on 21 Jul 2016, 14:34 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.

    K B 2 Replies Last reply 21 Jul 2016, 15:01
    0
    • A ankit thakar
      21 Jul 2016, 14:34

      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.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 21 Jul 2016, 15:01 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
      • A ankit thakar
        21 Jul 2016, 14:34

        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.

        B Offline
        B Offline
        beecksche
        wrote on 21 Jul 2016, 17:03 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.

        K 1 Reply Last reply 21 Jul 2016, 19:16
        -1
        • B beecksche
          21 Jul 2016, 17:03

          @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.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 21 Jul 2016, 19:16 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 21 Jul 2016, 19:31 last edited by
            #5

            Just use

            if (str == "...")
            {
               // ...
            }
            else if (str == "...")
            {
               // ...
            }
            //...
            else 
            {
                // ...
            }
            
            1 Reply Last reply
            0

            1/5

            21 Jul 2016, 14:34

            • Login

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