Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] Questins about creating a QML component using c++



  • Hello,

    I want to make a component which requires to use c++. I have read the docs about
    creating components via c++ but i have some questions.

    @class HelloQML : public QObject {
    //foo
    bool setHelloMessage(A *a_hello, B *b_hello);//question no1
    C * helloMessage();//question no2
    //will the above work?
    Q_INVOKABLE void doHello(char a, QString b, QString *c);//question no3
    }@

    @import foo

    HelloQML {

    helloMessage: {
    //Question no1
    //what should i add here??
    }
    //Question no3
    //will this work?
    doHello('a', "aa", "aaa");
    }@

    A,B,C are my classes which doesn't derive from QObject and I haven't add to them any QML relative code.

    thanks in advance


  • Moderators

    in order for any functions to be available in QML, you have to use either Q_INVOKABLE or Q_PROPERTY. So, "questions" 1 and 2 will not work. You are trying to use those methods as properties, which is wrong. You'll have to declare a property, and then add getter, setter methods, and a notification signal.

    About question 3 - this will also fail. You are trying to invoke a JS/C++ method directly in QML, which is not supported. You can invoke methods in assignments (like "height: myMethod(foo)") or in slots. So, here, for it to work, you'll have to use:
    @
    // inside HelloQML
    Component.onCompleted: doHello('a', "aa", "aaa");
    @

    Also, your import statement is wrong. It has to specify version number, IIRC.



  • [quote author="sierdzio" date="1327128947"]in order for any functions to be available in QML, you have to use either Q_INVOKABLE or Q_PROPERTY.
    [/quote]

    My mistake,I didn't mention about the existance of a Q_PROPERTY, before setHelloMessage there are

    @
    Q_OBJECT
    Q_PROPERTY(...)
    @

    [quote]
    About question 3 - this will also fail. You are trying to invoke a JS/C++ method directly in QML,[/quote]

    Correct.This was wrong

    I wanted to know if my code will work from the point of view that classes A,B,C doesn't derive from QObject.


  • Moderators

    [quote author="Giorgos Tsiapaliwkas" date="1327183176"]
    I wanted to know if my code will work from the point of view that classes A,B,C doesn't derive from QObject.
    [/quote]

    I don't think I've ever tried doing that, so I can't give you a definite answer. IIRC, as long as QVariant can work with these classes, you can use them in QML - C++ communication.



  • [quote author="Giorgos Tsiapaliwkas" date="1327072363"]Hello,

    @
    bool setHelloMessage(A *a_hello, B *b_hello);//question no1
    @

    @import foo

    HelloQML {
    onSomething:{
    helloMessage: {
    //what should i add here??
    //my setter has two parameters.
    }
    }@
    [/quote]

    Does anyone know what I should add??

    Will this work?

    @import foo

    HelloQML {
    onSomething:{
    helloMessage: {
    A:.....
    B:.....
    }
    }@


  • Moderators

    Properties can have only one parameter. They should represent getters and setters directly. I'm afraid your code is wrong again: you open a JavaScript block after "onSomething", which would any QML assignment inside invalid (like on lines 5., 6., and 7.).

    Here's a tip that would probably help you most: go ahead and try. Think of a simple, example application and code it to get basic experience. This would make solving this problem a lot easier for you, and also will be a source of fun.



  • thank you

    I will mark it as solved



  • Hello Sierdzio, your suggestions and tips helped me too. I would like learn from you if can start any forum related to Qt Quick and C++ integration. Nicely explained. Thanks a ton.

    Hello Giorgos, Thanks for asking your doubt, it clears my doubt too.

    Thanks and regards,
    Punit Tak


Log in to reply