How to write a switch statement for strings in Qt?

11,515

Solution 1

The only way to use switch() with strings is to use an integer-valued hash of a string. You'll need to precompute hashes of the strings you're comparing against. This is the approach taken within qmake for reading visual studio project files, for example.

Important Caveats:

  1. If you care about hash collisions with some other strings, then you'll need to compare the string within the case. This is still cheaper than doing (N/2) string comparisons, though.

  2. qHash was reworked for QT 5 and the hashes are different from Qt 4.

  3. Do not forget the break statement within your switch. Your example code missed that, and also had nonsensical switch value!

Your code would look like the following:

#include <cstdio>
#include <QTextStream>

int main(int, char **)
{
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
    static const uint red_hash = 30900;
    static const uint green_hash = 7244734;
    static const uint blue_hash = 431029;
#else
    static const uint red_hash = 112785;
    static const uint green_hash = 98619139;
    static const uint blue_hash = 3027034;
#endif

    QTextStream in(stdin), out(stdout);
    out << "Enter color: " << flush;
    const QString color = in.readLine();
    out << "Hash=" << qHash(color) << endl;

    QString answer;
    switch (qHash(color)) {
    case red_hash:
        answer="Chose red";
        break;
    case green_hash:
        answer="Chose green";
        break;
    case blue_hash:
        answer="Chose blue";
        break;
    default:
        answer="Chose something else";
        break;
    }
    out << answer << endl;
}

Solution 2

QStringList menuitems;
menuitems << "about" << "history";

switch(menuitems.indexOf(QString menuId)){
case 0:
    MBAbout();
    break;
case 1:
    MBHistory();
    break;
}
Share:
11,515
TSG
Author by

TSG

Telium Support Group (TSG): In addition to asking questions, we try to answer questions relating to Telematics and Telephony. Opinions expressed on Stack Exchange represent the views of Telium employees and may reflect an understanding of and bias for Telium products. (i.e. yes our views may be biased - but we try hard to keep opinions neutral!). If you have a question about a Telium product we prefer not to answer on stack exchange - please email us at [email protected] We are happy to answer your questions regarding Telium products and services.

Updated on June 04, 2022

Comments

  • TSG
    TSG almost 2 years

    I need to create the equivalent of a switch/case statement for strings in C++ with Qt. I believe that the simplest way is something like this (pseudo code)

    enum colours { red, green, blue };
    QString array[] colour_names = { "red", "green", "blue" };
    switch (color_names[user_string]) {
      case red: answer="Chose red";
      case green: answer="Chose green";
      case blue: answer="Chose blue";
      other: answer="Invalid choice";
    }
    

    But this doesn't take advantage of some of the features of Qt. I've read about QStringList's (to find the position of the string in a list of strings), and std:map (see How to easily map c++ enums to strings which I don't fully understand).

    Is there a better way to do a switch on strings?