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 {
        flagA,
        flagB,
        flagC
    };
    Q_DECLARE_FLAGS(amazing, testX)
    Q_DECLARE_OPERATORS_FOR_FLAGS(amazing)
    
    inline QDebug operator<<(QDebug &stream, const testX &flag){
        stream<<"LEL2";
        return stream;
    }
    inline QDebug operator<<(QDebug &stream, const amazing &flag){
        stream<<"LEL";
        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
    

    TIA


    Looks like adding :

    Q_DECLARE_METATYPE(testX)
    Q_DECLARE_METATYPE(amazing)
    

    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 2018

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.