Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to access std::vector<enum> in qml?

How to access std::vector<enum> in qml?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 713 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.
  • N Offline
    N Offline
    narae
    wrote on last edited by narae
    #1

    The TestMethod of c++ is called in qml.

    c++

    class Test : public QObject {
    
        Q_OBJECT
    public:
    
        enum TestEnum
        {
           RED = 0,
           BLACK = 0x1,
           YELLOW = 0x2,
           PINK = 0x4,
        };
    
        Q_ENUM(TestEnum)
    
        Q_INVOKABLE std::vector<TestEnum> testMethod();
    };
    
    Q_DECLARE_METATYPE(std::vector<Test::TestEnum>)
    

    main

    qmlRegisterType<Test>("mytest.test",1,0,"Test");
    qRegisterMetaType<std::vector<Test::TestEnum>>();
    

    qml

    import mytest.test 1.0
    
    Test{
            id: test
        }
    
    
    onClicked: {
      var result = test.testMethod();
    }
    

    It is return error.
    qml:384: Error: Unknown method return type: std::vector<Test::TestEnum>.

    How do I can solve this problem?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Use QVariantList instead of vector.

      (Z(:^

      N 1 Reply Last reply
      2
      • sierdzioS sierdzio

        Use QVariantList instead of vector.

        N Offline
        N Offline
        narae
        wrote on last edited by
        #3

        @sierdzio thanks for your answer. QVariantList is works.
        However,can't I use std::vector<TestEnum> in qml?

        sierdzioS 1 Reply Last reply
        0
        • N narae

          @sierdzio thanks for your answer. QVariantList is works.
          However,can't I use std::vector<TestEnum> in qml?

          sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          @narae said in How to access std::vector<enum> in qml?:

          @sierdzio thanks for your answer. QVariantList is works.
          However,can't I use std::vector<TestEnum> in qml?

          No, QML engine does not know how to convert it to JavaScript.

          (Z(:^

          N 1 Reply Last reply
          1
          • sierdzioS sierdzio

            @narae said in How to access std::vector<enum> in qml?:

            @sierdzio thanks for your answer. QVariantList is works.
            However,can't I use std::vector<TestEnum> in qml?

            No, QML engine does not know how to convert it to JavaScript.

            N Offline
            N Offline
            narae
            wrote on last edited by
            #5

            @sierdzio thanks

            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