Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-91552

Get confused by the description of QFlags

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 5.15.3
    • Core: Other
    • None
    • macOS

    Description

      Hi guys,

      I'm confused by the document of QFlags. 

      The document says that, because of C++ does not provide the type check for OR operation of enum type, So any enum value can be OR'd with any other enum value and passed on to a function that takes an int or uint.

      But, even if I use QFlags, I can also OR two different enum types and pass it to a function which take int . 

      // code placeholder
      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include<QQmlEngine>
      #include<QDebug>
      class ClassA{
       public:
        enum Option{
          UTF8,
          GBK,
          GB2312
        };
      
      };
      class ClassB{
       public:
        enum Option{
          UTF8,
          GBK,
          GB2312
        };
        Q_DECLARE_FLAGS(Options, Option)
      };
      Q_DECLARE_OPERATORS_FOR_FLAGS(ClassB::Options)
      void function(int option){
      
      }
      int main(int argc, char *argv[])
      {
        ClassA::Option optionA = ClassA::GBK;
        ClassB::Options optionB = ClassB::GBK;
        function( ClassA::GBK | ClassB::GBK);
        function(optionA | optionB);
        return 0 ;
      }
      

      And the example code does not match the document, it use a function which does not take int , instead it take a QFlag. 

      // code placeholder
      label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
      

      But even if a function take a enum type, the compiler will also report error when you pass a var calculated by OR different enum types. I don't need to use QFlags.

       

      So I want to know what is QFlags used for ? 

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            yorkxychen york123 chen123
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes