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. Global QMetaObject
Forum Updated to NodeBB v4.3 + New Features

Global QMetaObject

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 5.1k Views 1 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.
  • H Offline
    H Offline
    Henrik
    wrote on last edited by
    #1

    Is there a way to get a global QMetaObject from which ich can get the QMetaEnum for Qt::Alignment to get a String representation of my alignment or an Qt::Alignment from a String ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sabrog
      wrote on last edited by
      #2

      Of course:

      @
      #include <QtCore/QTextStream>
      #include <QtCore/QMetaEnum>

      struct StaticQtMetaObject : public QObject
      {
      static inline const QMetaObject& get() {return staticQtMetaObject;}
      };

      int main(int argc, char *argv[])
      {
      const QMetaObject& mo = StaticQtMetaObject::get();
      int index = mo.indexOfEnumerator("Alignment");
      QMetaEnum me = mo.enumerator(index);
      Qt::Alignment alignment = Qt::AlignLeft;
      QTextStream(stdout) << me.valueToKey(alignment) << endl;
      return 0;
      }
      @

      QT - Apple QuickTime
      Qt - Nokia's Qt Development Frameworks
      Apple is a competitor of Nokia, so QT != Qt

      1 Reply Last reply
      0
      • H Offline
        H Offline
        Henrik
        wrote on last edited by
        #3

        Thank you !

        I didn't know that a staticQtMetaObject exists.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Dolphin
          wrote on last edited by
          #4

          Am I understanding this right? This only gets enums from Qt name space right?

          How would I do the same thing for QListView::ViewMode??

          1 Reply Last reply
          0
          • H Offline
            H Offline
            Henrik
            wrote on last edited by
            #5

            There is a QListView::staticMetaObject

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dolphin
              wrote on last edited by
              #6

              Hmm, that is what I thought but my code does not work - am I doing something blonde? :-)

              @class StaticQtMetaObject : public QObject
              {
              public:
              static inline const QMetaObject& get() {return staticQtMetaObject;}
              };

              class StaticQtMetaAbstractItemView : public QAbstractItemView
              {
              public:
              static inline const QMetaObject& get() {return staticQtMetaObject;}
              };@
              @int CGuide::getEnumNumber(QString enumString, QString enumName)
              {
              int indexValue(0);
              int index = StaticQtMetaObject::get().indexOfEnumerator(enumName.toStdString().c_str());
              if(index >= 0)//the enum found
              {
              QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);
              indexValue = metaEnum.keyToValue(enumString.toStdString().c_str());
              }
              else
              {
              index = StaticQtMetaAbstractItemView::get().indexOfEnumerator(enumName.toStdString().c_str());
              if(index >= 0)//the enum found
              {
              QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);
              indexValue = metaEnum.keyToValue(enumString.toStdString().c_str());
              }
              }
              return indexValue >= 0 ? indexValue : 0;//always default - get negative numbers if code above fails
              }@

              1 Reply Last reply
              0
              • H Offline
                H Offline
                Henrik
                wrote on last edited by
                #7

                The second
                @QMetaEnum metaEnum = StaticQtMetaObject::get().enumerator(index);@

                should be

                @QMetaEnum metaEnum = StaticQtMetaAbstractItemView::get().enumerator(index);@

                I think you dont need the StaticQtMetaAbstractItemView class and you can just use QAbstractItemView::staticMetaObject.
                If you use it, it has to look like this:

                @class StaticQtMetaAbstractItemView : public QAbstractItemView
                {
                public:
                static inline const QMetaObject& get() {return staticMetaObject;}
                };@

                instead of
                @ static inline const QMetaObject& get() {return staticQtMetaObject;}@

                staticQtMetaObject is just a special case for the Qt namespace

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dolphin
                  wrote on last edited by
                  #8

                  So I should be able to write

                  @int index = QAbstractItemView::staticMetaObject.indexOfEnumerator(enumName.toStdString().c_str());
                  QMetaEnum metaEnum = StaticQtMetaAbstractItemView::get().enumerator(index);@

                  and no other code??

                  Tried this with:

                  enumName = QListView::Flow
                  and
                  enumName = Flow

                  but it never finds the enum

                  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