Assigning or changing int property using enum inside QML



  • 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


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.