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. c++ enum in QML print as string
QtWS25 Last Chance

c++ enum in QML print as string

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 821 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.
  • V Offline
    V Offline
    VeNToR
    wrote on 10 Oct 2021, 22:12 last edited by
    #1

    Hi;

    I need to print "ChQuDefaults.EChTest.ChTest (666)" to "ChQuDefaults.EChTest.ChTest (ChTest)" .
    I already tried as ChQuDefaults.EChTest.ChTest.toString() but always print "666".

    enum defined as in c++;

    class CChQuDefaults : public QObject {
    Q_OBJECT

    QML_VALUE_TYPE(ChQuDefaults)
    
    QML_ADDED_IN_VERSION(1,0)
    
    QML_SINGLETON
    
    public:
    	enum class EChTest : qint32
    	{
    		ChTest		= 666
    	};
    
    	Q_ENUM(
    		EChTest)
    
    // ...
    

    };

    Thanks...

    1 Reply Last reply
    1
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 11 Oct 2021, 05:52 last edited by
      #2

      Enums in QML always print their value. If you want to print the text, use QMetaEnum in C++.

      (Z(:^

      V 1 Reply Last reply 11 Oct 2021, 07:08
      0
      • S sierdzio
        11 Oct 2021, 05:52

        Enums in QML always print their value. If you want to print the text, use QMetaEnum in C++.

        V Offline
        V Offline
        VeNToR
        wrote on 11 Oct 2021, 07:08 last edited by
        #3

        @sierdzio thank you. I did before but I can't remember how ? :)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 11 Oct 2021, 07:18 last edited by
          #4
          QString result = QMetaEnum::fromType<EChTest>().valueToKey(int(EChTest::ChTest));
          

          Put it in some Q_INVOKABLE or slot and you will be able to call it from QML.

          (Z(:^

          V 1 Reply Last reply 11 Oct 2021, 07:49
          2
          • S sierdzio
            11 Oct 2021, 07:18
            QString result = QMetaEnum::fromType<EChTest>().valueToKey(int(EChTest::ChTest));
            

            Put it in some Q_INVOKABLE or slot and you will be able to call it from QML.

            V Offline
            V Offline
            VeNToR
            wrote on 11 Oct 2021, 07:49 last edited by
            #5

            @sierdzio sure, I mean in QML :)

            1 Reply Last reply
            0

            3/5

            11 Oct 2021, 07:08

            • Login

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