How to write a switch statement for strings in Qt?

5 Answers

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

switch(menuitems.indexOf(QString menuId)){
case 0:
case 1:

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?

MSalters solution is a good one but basically re-implements boost::assign::map_list_of. If you have boost, you can use it directly:

#include <boost/assign/list_of.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>

using boost::assign::map_list_of;

enum eee { AA,BB,CC };

const boost::unordered_map<eee,const char*> eeeToString = map_list_of
    (AA, "AA")
    (BB, "BB")
    (CC, "CC");

int main()
    std::cout << " enum AA = " << << std::endl;
    return 0;

As previously noted this is not a Qt problem, switch statements can only use constant expressions, look at the collection classes a QSet is a good solution

void initStopQwords(QSet<QString>& stopSet)
    // Ideally you want to read these from a file
    stopSet << "the";
    stopSet << "at";


bool isStopWord(const QSet<QString>& stopSet, const QString& word)
    return stopSet.contains(word);

(C++/ QT) Use a switch statement with strings by turning the strings into an int

If I ever find myself in a similar situation, I use a map to define a specific int from the given string.

For Example:

// The string you want to convert to an int
std::string myString = "stringTwo";

// The mapping that you set for string to int conversions
std::map<std::string, int> stringToInt = \
    {{"stringOne"  , 1},
     {"stringTwo"  , 2},
     {"stringThree", 3}};

// Here, myInt is define as 2
int myInt = stringToInt[myString];

Now you could put myInt into a switch case.

Try to look at this solution:

Unfortunately the description of this solution is avaliable only in Russian (at least I can't find one in English). It is based on computing hash of the string in compile-time. The only limitation it has is it supports strings with 9 character maximum length.

switch/case statement in C++ with a QString type

How can I use the switch statement with a QString?

You can't. In C++ language switch statement can only be used with integral or enum types. You can formally put an object of class type into a switch statement, but that simply means that the compiler will look for a user-defined conversion to convert it to integral or enum type.