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. Exposing object from C++ to qml returns null
Forum Updated to NodeBB v4.3 + New Features

Exposing object from C++ to qml returns null

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.9k 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.
  • 2 Offline
    2 Offline
    2beers
    wrote on last edited by
    #1

    Hello, I'm playing with qml and I'm trying to build a small program based on this documentation http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html where you can set properties and methods from c++ to qml.

    So... my problem is that is partially working.
    I'm sending the msg object to qml. Here is my qml code:

    @import QtQuick 2.0

    Rectangle {
    width: 250
    height: 100
    color : "#e5f3fe"
    border.color: "#114273"

    Component.onCompleted: {
        msg.author = "Jonah"  // invokes Message::setAuthor()
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            console.log("msg= "+msg);  // this returns msg=null
    

    // var result = msg.postMessage("Hello from QML")
    // console.log("Result of postMessage():", result)
    // msg.refresh();
    }
    }
    }@

    So... when I set @msg.author = "Jonah"@ everything works great, the setAuthor method is called, however when I click on mouse area the msg is null.

    Do you know why and how can I fix this?

    Thanks

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Is it the same example that you are trying from that doc as it is? It works for me. Which Qt Version/OS ?

      157

      1 Reply Last reply
      0
      • 2 Offline
        2 Offline
        2beers
        wrote on last edited by
        #3

        Hello, p3c0. Thanks for your reply.
        Qt 5.4, WIndows 7, MSVC 2013

        I tried to shrink my problem to a very simple program. I have a NotificationWindow that extends QWidget , and in that constructor I create a QQuickWidget attached to the NotificationWindow.
        The qml is rendered ok, the passed object is ok in Component.onCompleted but not on MouseArea.

        I'll attach the program source code to see what seems to be the problem.

        Download the program: QtQuickTest : https://app.box.com/s/5fg2hyh5y5sst4pxc4mu7aedex2g6hrj

        Thanks

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          That is because MessageBoard object which is created on stack in NotificationWindow constructor dies when the constructor exits. Create that object on heap instead, using new.

          157

          1 Reply Last reply
          0
          • 2 Offline
            2 Offline
            2beers
            wrote on last edited by
            #5

            Thanks. That was the problem. Now is working great.

            1 Reply Last reply
            0
            • p3c0P Offline
              p3c0P Offline
              p3c0
              Moderators
              wrote on last edited by
              #6

              Glad that its working :)
              Don't forget to delete that object or there would be a memory leak. Since it is in NotificationWindow widget you can pass it as a parent using this to MessageBoard so that when NotificationWindow gets deleted so does MessageBoard gets too.

              157

              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