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. QML custom notify event
Forum Updated to NodeBB v4.3 + New Features

QML custom notify event

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 1.0k 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.
  • G Offline
    G Offline
    Grunk
    wrote on last edited by Grunk
    #1

    Hello,

    I have a QObject with 2 properties :

    Q_PROPERTY(int isInitialized READ isInitialized WRITE setInitialized NOTIFY initChanged)
    Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged)
    

    I set this object to my QML like this :

    mQuickWidget->engine()->rootContext()->setContextProperty("splash", &mSplash);
    

    And then i my QML i use the properties like this :

    Item {
        MouseArea {
                anchors.fill: parent
                onClicked: {
                    splash.isInitialized= 1
    	        splash.message="test";
                }
           }
    }
    

    This part is working great. I can send value from C++ to QML by using my mSplash object or i can edit my mSplash object from QML by clicking.

    I would like in my QML to be able to "override" the initChanged NOTIFY to be able to do a specific action according to the value.
    I have tried something like

    Component.onCompleted: {
            splash.onInitChanged = function(){
                if(splash.isInitialized === 2) {
                    //Do something here
                }
            }
    }
    

    But it give me a TypeError : Cannot assign to read-only property.

    I'm a complete beginner with QT et QML , so i probably missing something obvious here.

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you looking for the connect statement ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Grunk
        wrote on last edited by Grunk
        #3

        Thanks , I finally found a solution which use a Connections object :

        Connections {
        	target: splash
        	onInitChanged : {
        		if(splash.isInitialized === 2) {
        			//Do something
        		}
        	}
        }
        

        It seems that the connect statement you are talking about is kind of the same thing right ? Should i use one more than an other ?

        By doing :

        Component.onCompleted: {
        	splash.onInitChanged.connect(doStuff);
        }
        
        function doStuff() {
        	if(splash.isInitialized === 2) {
        		//Do something
        	}
        }
        

        I have the exact same result , so i don't really know which solution to choose :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I'd say it depends whether you'll reuse doStuff elsewhere. Otherwise, coding style and personnel taste.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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