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. Assigning or changing int property using enum inside QML
QtWS25 Last Chance

Assigning or changing int property using enum inside QML

Scheduled Pinned Locked Moved QML and Qt Quick
c++qmlenum
2 Posts 1 Posters 1.1k 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.
  • S Offline
    S Offline
    Shardul Vyas
    wrote on last edited by
    #1

    Consider this simple enum class -

    #include <QObject>
    class BookTypes : public QObject
    {
    	Q_GADGET
    	Q_ENUMS(AllBooksType)    
    
    public:
    
    	enum AllBooksType{
    		eMagazine,
    		eReference,
    		eTextBook,
    		eThesis
    	};
    
    signals:
    
    public slots:
    
    };
    

    Type registration in main()

    qmlRegisterUncreatableType<BookTypes>("trial", 1, 0, "BookTypes", 
    "Don't create qml instance for BookTypes");
    

    And this is sample QML

    Rectangle {
    		id: rect
    		x: 100; y: 100
    		width: 100
    		height: 70
    		color: "PowderBlue"
    		border.color: "RoyalBlue"
    		border.width: 1
    		radius: 3
    
    		MouseArea{
    			x: 0; y: 0
    			height: parent.height
    			width: parent.width
    			property int bt: BookTypes.eTextBook //perfect. now bt is 2
    			onClicked: {
    				console.debug("old book type:- ")
    				console.debug(bt) //prints 2
    				console.debug("selected book type:- ")
    				bt = BookTypes.eReference //gives error - why ?
    				console.debug(BookTypes.eReference) //prints 'undefined'
    				console.debug(bt)
    			}
    		}
    	}
    

    This means - The enum is properly exposed, since it initializes bt successfully in -

    property int bt: BookTypes.eTextBook
    

    What i don't understand is - Why it is not accessible when i try to replace value of bt in handler.

    bt = BookTypes.eReference //gives error - why ?
    

    How do i pass such an enum as an argument of Q_INVOKABLE method ? Like:

    console.debug(BookTypes.eReference) //prints 'undefined'
    SomeObj.someCPPMethod(BookTypes.eReference) // sends 'undefined' and defaults to 0
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      Shardul Vyas
      wrote on last edited by
      #2

      Received answer on Stack Overflow.
      Note: The names of enum values must begin with a capital letter in order to be accessible from QML.

      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