qt - QML in C++ app or vice versa -


consider case of simple gui displaying output of rather elaborate calculation.

now write nice, custom gui using qml.
write background app in qt c++.

i'm sitting in front of qt documentation , wonder if i
1) should write qml application , somehow embed c++ classes in (which absolutely possible) or if i
2) should write c++ application , somehow embed qml gui in , modify qml properties classes (which again possible)

i wrote in c++ using qt widgets gui. want move gui qml , keep c++ classes though willing rewrite interface gui.

possible anser:

the marked solution below suggested keeping c++ classes , interface gui exclusively through signals , slots. ended main.cpp instantiates main working class , displays qml gui this:

qquickview viewer; viewer.setsource(qurl("./qml/main.qml")); viewer.show(); 

then added myclass , got me object connections:

myclass myclass; qquickitem* item = viewer.rootobject(); qobject::connect(item, signal(buttonclicked()), &myclass, slot(myslot())); qobject::connect(&myclass, signal(mysignal(qvariant)), item, slot(updategui(qvariant))); 

when implementing slots , signals in c++ classes must use qvariant objects transfer data. qml file implements signals e.g. clicked buttons , slots receive data display.

this hoping for. change non gui code interactions via signals , slots. can use both guis (qml / widgets) application.

just write core logic in c++, interface signals , slots, , can use same component widgets , qml.

it not rocket science, c++ logic allows usage c++ , qml, js logic - qml. c++ , qt api more sound solution, because js don't have access functionality of qt apis, few methods "ported" qml world. high performance data containers , execution performance in c++.

if need display results , console not enough, i'd rather keep qtwidgets, because adding declarative module slows compilation down significantly. widget module standalone now, adding "extra" module qtwidgets (in qt4 part of qtgui) lighter. after use widgets prototyping core logic, can implement qml interface , hook existing signals/slots/properties , bindings using them.

and no, don't embed qml in c++ classes, other way around, c++ more low-level layer, used create qml components. actual instantiation, can go both ways - if register qobject based class qml engine, can instantiate in qml. or instantiate class in c++ , make available in qml context - doesn't matter. if need single object, better instantiate in c++ in main() function , make available in qml context, if components intend instantiate lot - create qml component.

you prototype core logic sj in qml , later port c++ if want too. twice effort. i'd keep logic c++ , gui qml, unless simple, don't need c++.

but "elaborate calculation" want in c++. every time calculation completed, can emit signal, , automatically display result whatever slot signal connected, in widget or in qml, or both @ same time.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -