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. Custom type with properties like QML color
Qt 6.11 is out! See what's new in the release blog

Custom type with properties like QML color

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.8k Views 2 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.
  • SeeLookS Offline
    SeeLookS Offline
    SeeLook
    wrote on last edited by
    #1

    Hi all,

    I can register custom type (not QObject derivative) and with QGadget & Q_INVOKABLE macro get access to its functions,
    but
    is there any way to have similar functionality like QML color, to have property-like access:

    property color someColor: "red"
    // then we have access to its "props" like:
    someColor.r // red component
    someCalor.a // alpha
    

    Is it possible for custom type which has to be light like in this example QColor/QML color is?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi! Yes that's possible:

      class Pet
      {
          Q_GADGET
          Q_PROPERTY(QString name READ name WRITE setName)
          Q_PROPERTY(int age READ age WRITE setAge)
          
      public:
          QString name() const { return m_name; }
          void setName(const QString &name) { m_name = name; }
      
          int age() const { return m_age; }
          void setAge(int age) { m_age = age; }
      
      private:
          QString m_name;
          int m_age = 0;
      }
      
      Q_DECLARE_METATYPE(Pet)
      
      1 Reply Last reply
      2
      • SeeLookS Offline
        SeeLookS Offline
        SeeLook
        wrote on last edited by
        #3

        @Wieland Thanks a million.

        1 Reply Last reply
        0
        • SeeLookS Offline
          SeeLookS Offline
          SeeLook
          wrote on last edited by SeeLook
          #4

          By the way,
          Is there any difference for such a case

          //cpp
          Q_PROPERTY(QString name READ name WRITE setName)
          QString name() const { return m_name; }
          void setName(const QString &name) { m_name = name; }
          //QML
          property Pet pet
          pet.name
          

          or

          //cpp
          Q_INVOKABLE QString name() const { return m_name; }
          //QML
          property Pet pet
          pet.name()
          

          I mean speed or memory consumption, which one is more preferable?

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            @SeeLook said in Custom type with properties like QML color:

            Q_INVOKABLE

            It's both correct and useful code. The only difference is that if you add Q_INVOKABLE then that function is also registered to the meta object system / can also be called from QML. Speed: don't know, sry.

            1 Reply Last reply
            2
            • P Offline
              P Offline
              patrik08
              wrote on last edited by
              #6

              Long time a go i register all standard xsl:fo color same color from openoffice or apache fop xml https://xmlgraphics.apache.org/fop/0.95/extensions.html

              i register as FopColor:FopColor

              record("black" 	,QColor( 0, 0, 0,255));
              	record("blanchedalmond"	,QColor(255, 235, 205,255));
              	record("blue"	,QColor( 0, 0, 255,255));
              	record("blueviolet" 	,QColor(138, 43, 226,255));
              	record("brown"	,QColor(165, 42, 42,255));
              	record("burlywood"	,QColor(222, 184, 135,255));
              	record("cadetblue"	,QColor( 95, 158, 160,255));
              	record("chartreuse"	,QColor(127, 255, 0,255));
              	record("chocolate"	,QColor(210, 105, 30,255));
              	record("coral" 	,QColor(255, 127, 80,255));
              	record("cornflowerblue"	,QColor(100, 149, 237,255));
              	record("cornsilk" 	,QColor(255, 248, 220,255));
              	record("crimson"	,QColor(220, 20, 60,255));
              	record("cyan"	,QColor( 0, 255, 255,255));
                     and many moore...
              

              read:
              https://github.com/pehohlva/DocSpeacker/blob/master/3rdparty/oldtimer/FoColorName.cpp
              https://github.com/pehohlva/DocSpeacker/blob/master/3rdparty/oldtimer/FoColorName.h

              all this color you find inside openoffice libreoffice format to read odt file ... qt can write odt file (but not read) i suppose now in qt5 have moore specification...
              inside Okular source you find out the same color specification.. the list is very long...

              1 Reply Last reply
              0
              • SeeLookS Offline
                SeeLookS Offline
                SeeLook
                wrote on last edited by
                #7

                Thank You for the answers.
                It clarified steps to take in my code.

                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