[SOLVED] Qt Quick (QML) and C/C++ example



  • Ok, I did some searching but could not find any good examples with short descriptions. Most of pages go into a lot of detail and require a significant object oriented programming background.

    I have a decent C programming background but not much C++ or Java and I quickly get lost with these C++ examples. I am used to the Visual C way of programming where gui object's action such as BUTTON_CLICK would be directly linked to the C/C++ code. I was hoping that Qt/QtQuick provides similar solution, but I failed to located a simple example.

    My question is simple:
    Is it possible to do a QtQuick(QML) and C combo on embedded device? Prefer QtQuick due to the graphic flexibility and easy animations/transitions.

    Project goal: use QtQuick for a GUI and C for the "code behind".
    The "code behind" would control the following:

    • I2C bus
    • change the state of discrete outputs
    • generate data to be sent to QML for the display
      I can handle the C code and embedded side of things, but I cannot figure out how to execute the C code after a QML defined button is clicked.

    Thx!



  • Did u try the "hello world" that cames from Qt Installation examples ?
    I think you will get it there .. is simple



  • Are you sure that "hello world" example goes into mixing C++ and QML? I would appreciate the exact path to the example you have mentioned.

    I am using qt 4.8.4 and I was unable to locate any "hello world" code in the "examples" folder which would go over the usage of C/C++ with QML.



  • There are two ways to connect you C++ code with QML. One is to add instance of your class to QML context, second is to register your class as QML type.
    Lets have class like this:
    @
    class MyClass : public QObject {
    Q_OBJECT
    public slots:
    void doSomething();
    }
    @
    Now to add this class to QML context you have to do something like this in you main method (or anywhere else, where you have access to QmlApplicationViewer instance):
    @
    QmlApplicationViewer viewer;
    ...
    viewer.rootContext()->setContextProperty("MyObject", new MyClass());
    @
    In QML you can do:
    @
    Item{
    Component.onCompleted: MyObject.doSomething()
    }
    @
    To register class to QML you need to do something like this in your main method (or somewhere else):
    @
    qmlRegisterType<MyClass>("my.utils",1,0,"MyClass");
    @
    And in QML:
    @
    import my.utils 1.0
    Item{
    Component.onCompleted: myObject.doSomething()
    MyClass{
    id: myObject
    }
    }
    @



  • Very good post portoist! Thanks!



  • Does this work with existing Qt classes? Say the QUdpSocket class? Is it possible that someone could give an example of how to bind that class into QML? Most of my experience is with embedded processors and C, so this is a little abstract to me... including the tutorials.

    basically is it as easy as this?

    @ import QUdpSocket @

    and then, to call the writeDatagram function from a button, I just simply need the following (using general function description)?

    @Button {
    id: autoupdate
    x: 340
    y: 56
    text: "AutoUpdate"
    signal clicked
    onClicked: QUdpSocket.writeDatagram()
    }@



  • learn a lot


  • Moderators

    [quote author="jediengineer" date="1391461698"]Does this work with existing Qt classes? Say the QUdpSocket class? Is it possible that someone could give an example of how to bind that class into QML? Most of my experience is with embedded processors and C, so this is a little abstract to me... including the tutorials.

    basically is it as easy as this?

    @ import QUdpSocket @

    and then, to call the writeDatagram function from a button, I just simply need the following (using general function description)?

    @Button {
    id: autoupdate
    x: 340
    y: 56
    text: "AutoUpdate"
    signal clicked
    onClicked: QUdpSocket.writeDatagram()
    }@[/quote]Hi Jediengineer, please don't post the same thing twice. I've replied to your original post: http://qt-project.org/forums/viewthread/37907/



  • JKSH, sorry, I didn't see your reply when I posted this. I'll keep it in one posting from here on out. Thanks!


Log in to reply
 

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