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. QVariant to/from QFlag
Forum Updated to NodeBB v4.3 + New Features

QVariant to/from QFlag

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.1k Views 3 Watching
  • 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.
  • shaveraS Offline
    shaveraS Offline
    shavera
    wrote on last edited by
    #1

    I'm unit testing a function for a table model, where it emits 'headerDataChanged'. One of the fields in this signal is a 'Qt::Orientation' flag. When a QSignalSpy picks it up, the QVariant for the orientation field is an invalid one. I can't cast back to Qt::Orientation. Do I have to locally declare orientation as a metatype? It seems odd it wouldn't just treat it as an int or something.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you show the code from that part ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        In Qt 5.7 it seems happy about it
        alt text

          QVariant test;
          test.setValue(Qt::Orientation::Vertical);
          Qt::Orientation back = test.value<Qt::Orientation >();
          qDebug() << "i see " << back;
        
        
        shaveraS 2 Replies Last reply
        2
        • mrjjM mrjj

          Hi
          In Qt 5.7 it seems happy about it
          alt text

            QVariant test;
            test.setValue(Qt::Orientation::Vertical);
            Qt::Orientation back = test.value<Qt::Orientation >();
            qDebug() << "i see " << back;
          
          
          shaveraS Offline
          shaveraS Offline
          shavera
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            In Qt 5.7 it seems happy about it
            alt text

              QVariant test;
              test.setValue(Qt::Orientation::Vertical);
              Qt::Orientation back = test.value<Qt::Orientation >();
              qDebug() << "i see " << back;
            
            
            shaveraS Offline
            shaveraS Offline
            shavera
            wrote on last edited by shavera
            #5

            @mrjj It may be that 5.7 has the appropriate behaviour, but 5.5 doesn't seem to. For now, my project seems to be settled on 5.5, so maybe this is just a limitation I have to deal with, but is already fixed in newer Qt versions.

            For a little tester I made a class:

            class Dummy : public QObject
            {
                Q_OBJECT
            public:
                enum DummyOpt{
                    Alpha = 0b01,
                    Beta = 0b10
                };
                Q_DECLARE_FLAGS(DummyOpts, DummyOpt)
            
                explicit Dummy(QObject *parent = nullptr);
            
            signals:
                void optionsChanged(DummyOpts);
            
            public slots:
                void setDummyOpts(DummyOpts o);
            };
            
            Q_DECLARE_OPERATORS_FOR_FLAGS(Dummy::DummyOpts);
            

            setDummyOpts just emits the optionsChanged signal. In main, I hooked up a QSignalSpy like:

                Dummy d;
                QSignalSpy optionsSpy{&d, &Dummy::optionsChanged};
                d.setDummyOpts(Dummy::Alpha);
            
                QVariant variant = optionsSpy.at(0).at(0);
            
                qDebug() << variant.value<Dummy::DummyOpts>();
            

            And when I compile, I get:

            error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system
            Q_STATIC_ASSERT_X(QMetaTypeId2<T>::Defined, "Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system");

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @shavera said in QVariant to/from QFlag:

              Q_DECLARE_METATYPE

              I dont see you register your own enum anywhere?
              Q_DECLARE_METATYPE (Dummy::DummyOpt);

              1 Reply Last reply
              1

              • Login

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