Important: Please read the Qt Code of Conduct -

QDebug & QFlags - pretty text?

  • Hey

    One of the odd ones... so if I add a macro to my enum class to treat it as flag using qt system. How can I then configure its print to be more "pretty" ?

    enum class testX {
    Q_DECLARE_FLAGS(amazing, testX)
    inline QDebug operator<<(QDebug &stream, const testX &flag){
        return stream;
    inline QDebug operator<<(QDebug &stream, const amazing &flag){
        return stream;

    The example above + this code yeld this result :

        amazing f;
        f = testX::flagA | testX::flagC;
        qDebug() << f; /// QFlags(0x1|0x4|0x20)  < how to make this pretty? - also why do I get 3 items and not 2? (its print from different test but using 2 items as well - values here will beoff)
        qDebug() << testX::flagA; /// gives correct LEL2 output from overload above


    Looks like adding :


    Have helped a bit, at least with QVariant conversions... also my qDebug gets called properly on
    inline QDebug operator<<(QDebug &stream, const amazing &flag){}

    but when I try :

    inline QDebug operator<<(QDebug &stream, const amazing &flag) {
        stream << "(";
        switch (flag) {
            case testX::flagA: {
                stream << "flagA ";

    I get error with
    value of type xx is not implicitly convertible to QFlags::int(aka int)
    and I have no idea what to do with it O_o.

    Seems like doing

    inline QDebug operator<<(QDebug &stream, const amazing &flag) {
        stream << "(";
            if(flag.testFlag(testX::flagA) {
                stream << "flagA ";

    Sorta works... but I'm getting weird flags.. do I need to number each item in my enum 1/2/4/8/16 etc etc? /// seems to be the case... also there seem to be a limit... I cant have 100+ flags :- (

  • Qt Champions 2019

    @Dariusz said in QDebug & QFlags - pretty text?:

    I cant have 100+ flags :- (

    Why do you want to have so many flags in one property?
    "do I need to number each item in my enum 1/2/4/8/16" - yes, if you want to combine several flags in one variable. Flags use OR to set a flag, AND to unset the flag. This is why each flag must have its own bit in a the integer value:

    // Set flag 0x01 with some other flags already set
    01010000 OR 00000001 = 01010001
    // Unset same flag keeping other flags
    01010001 AND 11111110 = 01010000

    "but I'm getting weird flags" - what do you mean by that?

  • try:

    inline QDebug operator<<(QDebug &stream, const amazing &flag) {
        if (!flag) {
            return stream << testX(0x00);
        amazing l_copy = flag;
        testX l_bit = testX(0x01);
        stream.noquote().nospace() << "(";
        forever {
            if (l_copy.testFlag(l_bit)) {
                stream.noquote().nospace() << l_bit;
                l_copy.setFlag(l_bit, false);
                if (!l_copy) break;
                stream.noquote().nospace() << "|";
            l_bit = testX(qint32(l_bit) << 1);
        stream.noquote().nospace() << ")";
        return stream;

Log in to reply