Friday, 15 February 2013

qt - Binding C++ to QML code when using Components -



qt - Binding C++ to QML code when using Components -

---edited url , changed dynamic part compilable---- (using qt 5.3) tried create compact sample, still big post files here separately, added link "uploaded.to" cannot seem attach zip file here :-(( (warning, spam links , / or waiting time, improve fileshare site recommend ?

here link "bindtest.zip" via uploaded.com, beware of spam/ugly pix:

http://ul.to/lqemy5jx

okay, seek post essence of files here anyways:

i tried create simple class in c++ containing stringlist , index. instantiated 2 objects of class , exposed them via "setcontextproperty"

this should used in qml initialize listview , in sync it. whenever user changes index in qml, c++ should notified , vice versa.

so when create 2 component qml files using hardwired names set in "setcontextproperty" seems work fine.

but life of me cannot create single component file , pass dataobject parameter, not know how it, although tried.

my "final" target ist create qml object dynamically , pass dataobject it, not work either :-(

so here comes, code snippets of sample project:

declaring oh-so-simple class (dataobject.h)

#ifndef dataobject_h #define dataobject_h #include <qobject> #include <qdebug> class dataobject : public qobject { q_object q_property( int index fellow member m_index notify indexchanged ) public slots: int count() const { homecoming m_elements.count(); } qstring at(int idx) const { homecoming m_elements.at(idx); } public: void setindex(int theint) { m_index = theint; } signals: void indexchanged(int); public: // lazy write accessors sample, create public qstringlist m_elements; private: int m_index; }; #endif // dataobject_h

registering in main.cpp:

qmlregistertype<dataobject>("bindtesttypes", 1, 0, "dataobject");

here part of "dialog.cpp" initializes , exposes 2 dataobects:

//preparing first list m_firstdo.m_elements = qstringlist() << "a" << "b" << "c" << "d"; m_firstdo.setindex(0); //preparing sec list m_seconddo.m_elements = qstringlist() << "a" << "b" << "c" << "d"; m_seconddo.setindex(3); //publish 2 dataobjects m_engine.rootcontext()->setcontextproperty( "cppdatalist_1", &m_firstdo); m_engine.rootcontext()->setcontextproperty( "cppdatalist_2", &m_seconddo);

here qml file "showlists.qml" should show 2 listvies on top of each other, commented 2 not working approaches love work, dynamic one:

import qtquick 2.2 import qtquick.window 2.1 import bindtesttypes 1.0

window { visible: true width: 200 height: 400 rectangle{ anchors.fill: parent //dynamic: not work :-( // need click on create // rectangle{ // id:upperlist // anchors.top: parent.top; // anchors.left: parent.left // width:200 // height:200 // mousearea{ // anchors.fill: parent // onclicked: { // var component = qt.createcomponent("simplelist.qml"); // var dyncbb = component.createobject(parent, {"thedo": cppdatalist_1}); // } // } // } // rectangle{ // id:lowerlist // anchors.bottom: parent.bottom; // anchors.left: parent.left // width:200 // height:200 // mousearea{ // anchors.fill: parent // onclicked: { // var component = qt.createcomponent("simplelist.qml"); // var dyncbb = component.createobject(parent, {"thedo": cppdatalist_2}); // } // } // } //static: not first selection isnt working anyways... // simplelist { // id:upperlist // property dataobject thedo: cppdatalist_1 // anchors.top: parent.top; // anchors.left: parent.left // } // simplelist { // id:lowerlist // property dataobject thedo: cppdatalist_2 // anchors.bottom: parent.bottom; // anchors.left: parent.left // } //hardwired works, not workable rather complex project... simplelist1 { id:upperlist anchors.top: parent.top; anchors.left: parent.left } simplelist2 { id:lowerlist anchors.bottom: parent.bottom; anchors.left: parent.left } } }

here first hardwired simplelist1.qml works fine, second:

import qtquick 2.2 listview { id: list_view width: 200 height: 200 currentindex: cppdatalist_1.index model: cppdatalist_1.count() delegate: rectangle { height: 20 width: 200 text { text: cppdatalist_1.at(index); color: (list_view.currentindex === index)?"red":"black" } mousearea{ anchors.fill: parent; onclicked: list_view.currentindex = index } } oncurrentindexchanged: cppdatalist_1.index = currentindex; }

this "simplelist.qml" cannot seem work:

import qtquick 2.2 import bindtesttypes 1.0 rectangle { listview { id: list_view property dataobject thedo width: 200 height: 200 currentindex: thedo.index model: thedo.count() delegate: rectangle { height: 20 width: 200 text { text: thedo.at(index); color: (list_view.currentindex === index)?"red":"black" } mousearea{ anchors.fill: parent; onclicked: list_view.currentindex = index } } oncurrentindexchanged: thedo.index = currentindex } }

so, can of help me solved ??

if dare follow uploaded link , run sample can see 1 more glitch. displays 2 windows, 1 qqquickwindow , widget. in widget can alter indexes in qml window. @ first in sync qml window not updated anymore changing index in widget, hope glitch , not general error made.

greetings & help !

nils

argh, found problem, did simple mistake:

the property want set in simplelist component has in root object, instead of this:

rectangle { listview { id: list_view property dataobject thedo ...

it has done way:

rectangle { property dataobject thedo listview { id: list_view ...

wow, thats easy solution (seemingly) complex problem.

greetings,

nils

c++ qt dynamic binding qml

No comments:

Post a Comment