c++qtenumsenum-class

I want to make a C++ enum of QString formats to display a QTime


I've been working with C++ on a Time class in Qt and I need to write an enum class that contains the formats that I use in showTime(). Because of the nature of the QString formats I've been getting the following error, the value is not convertible to 'int'. I would gladly appreciate any help or suggestions.

P.S: Here's the code:

enum class TimeFormat {
    a = "hh:mm:ss",
    b = "hh:mm",
    c = "H:m:s a"
};

class Time {
public:
    Time();
    ~Time();

    void setTime(QTime t);
    QString showTime(){
         return time->toString(format);
    }
private:
    QTime* time;
    TimeFormat format;
};

Solution

  • I used a switch statement

    enum class TimeFormat {
        format1,
        format2,
        format3,
    };
    
    class Time {
    public:
        Time();
        ~Time();
    
        void setTime(int h, int m, int s);
        QString showTime() const{
            QString f;
            switch (format) {
                case TimeFormat::format1:
                f="hh:mm:ss";
                break;
             case TimeFormat::format2:
                f = "hh:mm";
                break;
             case TimeFormat::format3:
                f= "H:m:s a";
                break;
             }
             return time->toString(f);
        }
    
        void changeFormat(TimeFormat f);
    private:
        QTime* time;
        TimeFormat format;
    };