'pure' C++ and QML



  • Hello,

    About year ago I learned (on my own) C++ and managed to complete my goal application - console app taking optional switches to control input/output.

    Next goal was to learn GUI and that's where it became harder.
    Keep in mind that I am self learner and probably approaching learning curve from other end that it would be suggested, i.e. I had goals and was searching methods how to achieve those goals.

    After some searching of best approach I decided that QML is the best approach for me. Particularly I liked that I could use QML and JS for interface and combine it with C++ used for backend. Sounded perfect!

    I started learning QML (most helpful was http://qmlbook.org) and I made 'perfect' GUI.

    Now I'm trying to combine my C++ code with QML and that's where I hit the wall.
    It seems like I need to completely rewrite my existing 'pure' C++ code to Qt C++ in order to be able to use it...
    Is that right?

    Are there any methods to use 'pure' C++ classes/functions instead of learning Qt C++ ?
    Please direct me to easiest/best ways to do it.



  • You mean you want to call that pure C++ classes/functions from QML GUI, right?

    If so, you can write a "facade class":http://en.wikipedia.org/wiki/Facade_pattern derived from QObject and expose it to QML with registerXXX function.

    Pure C++ class has no the Qt Meta-Object System and can not be used in QML.



  • Thanks, will have a look into that.
    However as for a noob like me that looks very complicated ;)



  • It would be better if you add Qt spices into your 'pure' C++ to integrate with QML, rather than introducing another layer of facade/adapter. I believe you don't want to keep writing that extra layer in your future projects.

    You can still keep your methods and logic but of course some modifications are need, like:

    inheriting your class from QObject

    change some 'pure' data type into Qt's flavor, eg: change your existing string to QString.

    make some methods become Qt slots so that Javascript in QML can call them

    You can check out the WeatherInfo example which illustrates the integration of C++ and QML: http://qt-project.org/doc/qt-5/qtpositioning-weatherinfo-example.html

    You should have a good read on "Integrating QML and C++":http://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html too.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.