[Solved] Exporting functions and constants from C++ to QML



  • Hi all.
    Probably thi will be considered a very noob question, but that's it.
    I have a QML app that make use of some constants defined on a javascript file. What I would like to do is to remove those constants from public visible javascript and put them into the c++ code. know that a possible solution would be to expose them with the rootContext()->setContextProperty(...) method, but... What if I would like to have a C++ class that use some functions in order to compute those constants? I mean:
    al the constants are strings, so I would like to have, in the QML code, something like:
    myClass.getUrl1(), myClass.getUrl2(), and so on
    but withouth the need to define a QML object
    @MyClass {
    id: myClass
    }@
    in the QML code. Is this possible? :)



  • Hi,
    You can just register a C++ class into QML with the required slots (or invokable methods) or expose an instance of such a class through setContextProperty. All you need is inherit from QObject.
    Good luck!



  • Wll, I didn't think I could export some classes instances as context properties, but..in effect the function accept an object as argument... :)

    so the idea is to have something like:

    @MyClass.h
    class MyClass : public QObject {
    Q_OBJECT
    public:
    explicit MyClass(QObject *parent = 0);
    QString getAnUrl();
    QString getAnotherUrl(QString parameter)
    private:
    const QString aConstant = "http://anurl";
    const QString anotherConstant = "http://anotherurl";
    }
    @
    @MyClass.cpp
    MyClass::MyClass(QObject *parent) : QObject(parent) {
    }

    QString MyClass::getAnUrl() {
    return this.aConstant;
    }

    QString MyClass::getAnotherUrl(QString parameter) {
    return this.anotherConstant + "?aparameter=" + parameter;
    }
    @

    @main.cpp
    Q_DECL_EXPORT int main(int argc, char *argv[]) {
    ....
    MyClas myClass;
    viewer.rootContext()->setContextProperty("myClass", myClass);
    ....
    }
    @

    and then to use myClass.getAnUrl() in QML and js file.

    Right? :P


  • Moderators

    Yes. Now you only need to add Q_INVOKABLE in front of your method declarations in the header file.



  • Ok! Thanks... And now let's do some programming :P


  • Moderators

    May the code be with you.

    You'll also have to correct some errors in your c++ code, but I trust the compiler to point them out for you.



  • Well, yep, I've written down the code quickly ;)



  • Well... It worksss!!! Thaks guys ;)


  • Moderators

    I'm happy to hear that :) And you even marked the thread as solved - perfection itself. Happy further coding.



  • Ok, I have one more question with respect to this :)
    What I have done is something like:
    @Q_INVOKABLE QString getUrl(QString urlParameter);@

    But in many tutorials I have seen now, the use
    @Q_INVOKABLE QString getNewsUrl(const QString &urlParameter);@

    Which one is more correct?

    Also. Having javascript "var" variables that contains integers, is there a way to pass this types to C++ instead of strings? Or should I convert them in C++? I mean... Using
    @Q_INVOKABLE QString getNewsUrl(int urlParameter);@
    gives me no right values in urlParameter variables...

    :)



  • [quote author="Ices_Eyes" date="1346753251"]Ok, I have one more question with respect to this :)
    What I have done is something like:
    @Q_INVOKABLE QString getUrl(QString urlParameter);@

    But in many tutorials I have seen now, the use
    @Q_INVOKABLE QString getNewsUrl(const QString &urlParameter);@

    Which one is more correct?
    [/quote]
    Why would you assume the one is 'more' correct than the other. By what measure exactly?

    Some coding guidelines tell you to use const references for function arguments that as used unchanged by the function, and in that context using the const QString& might be more correct. But not everybody uses this guideline. For efficieny, there is very little gain here, as QString is implicitly shared and thus very fast to copy as a value.


  • Moderators

    [quote author="Ices_Eyes" date="1346753251"]
    Also. Having javascript "var" variables that contains integers, is there a way to pass this types to C++ instead of strings? Or should I convert them in C++? I mean... Using
    @Q_INVOKABLE QString getNewsUrl(int urlParameter);@
    gives me no right values in urlParameter variables...
    :)[/quote]

    As Andre already covered the top part, I'll take on the bottom one. QML automatically translates JS values to C++ ones. So, for int you get c++ int, for real you get Qt's qreal, for string it's QString, for arrays is complicated (as you can store anything there. But basically QObjectList tends to work in most cases, or QVariantList etc.). There is no need for conversion of those basic types.



  • So, I need to understand why
    @
    var parameter = 1;
    ...
    myClass.myFunc(parameter)
    @
    works if myFunct is defined with QString and does not work if defined with int... ;)

    Thanks guys!!! :)



  • Damn!!! My bad... It was a conversion problem in C++ of integer to string!!! Damn, too much codin in Java and Javascript, I have completly forgotten C!!! :(


  • Moderators

    This is indeed strange. Be sure to make a full app rebuild (clean, run qmake, build) just to make sure that MOC updates the definitions. It quite definitely works for me. Alternatively, you can try using QVariant in c++ declaration, and then calling ::toInt() on the value - but that should not be necessary.



  • Solved with a QString::number(parameter)...

    Damn me... :P


  • Moderators

    Happens to everybody :) That took me by surprise a few times, too. I tend to forget about ::number() especially when I'm tired and after prolonged QML-only phases :D


Log in to reply
 

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