qt - Passing an object from QML to C++ -
i working on simple app demonstrate integration of c++ qml have problems. in nutshell want create c++ object in qml on fly , pass c++ processing. here code.
import qtquick 2.4 import qtquick.window 2.2 import test 1.0 window { visible: true mainform { anchors.fill: parent mousearea.onclicked: { console.log("clicked") var student = qt.createcomponent("student") student.name = "hello frome qml"; console.log( student.name ) // school.addstudent( student ) } } }
student.h
#ifndef student_h #define student_h #include <qobject> class student : public qobject { q_object qstring m_name; public: explicit student(qobject *parent = 0); ~student(); q_property(qstring name read name write setname notify namechanged) qstring name() const { return m_name; } signals: void namechanged(qstring arg); public slots: void setname(qstring arg) { if (m_name == arg) return; m_name = arg; emit namechanged(arg); } }; #endif // student_h
now when addstudent()
school class, there there trouble. concern .cpp file below.
#include "school.h" #include <qdebug> school::school(qobject *parent) : qobject(parent) { } school::~school() { } // why not work? cause compiler error..can't access private members of qobject //void school::addstudent(student student) //{ //// students.append( &student ); //// qdebug() << "added"; //// qdebug() << student.name();// << " added school"; //} void school::addstudent(student * student) { students.append( student ); qdebug() << "added"; qdebug() << student->name();// if line there, breaks application }
the questions in comments inside code summaries:
why addstudent() crashes when try access student.name? can access though in qml file after set it.
why project not compile if pass
student
by value seen in code?
additional thoughts
it seems may have c++ object getting destroyed time c++ function execute. life cycle of c++ object created in qml? when gets destroyed? lifetime of object issue here?
assume register student
type qml via qmlregistertype
in cpp file.
in qml, qt.createcomponent("student")
not create student
object qqmlcomponent
. try set properties other name
, still works since object is not student
. instead, should create object pre-defined component via component.createobject:
mainform { id: mainform mousearea.onclicked: { var student = studentcomponent.createobject(mainform); student.name = "hello frome qml"; school.addstudent( student ); } component { id: studentcomponent student{} } }
and works fine now.
back code,
//qml var student = qt.createcomponent("student") student.name = "hello frome qml"; console.log( student.name ) //
no, it's not good. student.abcdefg = "hello qml"
works, too. add console.log(student)
, can see student
not student
.
why addstudent() crashes when try access student.name? can access though in qml file after set it.
it crashed since object passed qml not pointer student
. qml engine passes null pointer function. accessed in qml not student
.
why project not compile if pass
student
value seen in code?
because student
qobject
, not copyable. pass pointer instead.
what life cycle of c++ object created in qml? when gets destroyed? lifetime of object issue here?
the lifetime not issue in qml since created component, not object. when creating student
object in answer,
var student = studentcomponent.createobject(mainform);
the newly created object sets mainform
it's parent. object won't deleted until mainform
released. except delete explicitly.
Comments
Post a Comment