Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDebug & QFlags - pretty text?
QtWS25 Last Chance

QDebug & QFlags - pretty text?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qflagqflagsqdebug
3 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dariusz
    wrote on 21 Apr 2019, 19:36 last edited by Dariusz
    #1

    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 :- (

    J 1 Reply Last reply 23 Apr 2019, 05:33
    0
    • D Dariusz
      21 Apr 2019, 19:36

      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 :- (

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 Apr 2019, 05:33 last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • I Offline
        I Offline
        israelins85
        wrote on 29 Jan 2020, 17:55 last edited by
        #3

        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;
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved