How do I actually execute C++ code in Qt Quick?



  • Sorry in advance for being such a noob, but... I ragequit on google after searching this for a while :(
    I've made a function in c++ that looks like so:

    @string convert(const string& userstr)
    {
    buff = userstr.front();
    resultstr = userstr;
    resultstr.front() = '\b';
    resultstr = resultstr + buff + "ay";
    return resultstr;
    }@

    What it's supposed to do is take in a string (from QT's text input) and manipulate it by moving its front letter to the back and adding "ay" ("pig latin":http://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/)

    And I think (haven't been able to test it yet) that this code should work. In QT I have textinput with the id: "uinput" and plain text with the id: "output".

    And I want on mouse click (onClicked) to run the text from "uinput" through this function and spit out "resultstr" into "output".

    This is how I tried to do it (in mousearea):

    @ onClicked: {
    output.text = convert.(uinput);
    }@

    but it just tells me I've got unexpected tokens, please point me in the right direction :)



  • Hi, I would suggest using JavaScript for this simple function, or why do you want to use c++ for this?

    @
    function toPigLatin(s) {
    return s.substr(1) + s.charAt(0) + "ay"
    }
    @
    I don't know if I got the algorithm right, but this is how you can do it with pure JS in QML.

    If you really want or have to use c++ you can do that, you can register a QObject class to the QMl engine with "qmlRegisterType":http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterType and than create Objects of that class from QML, or register a singleton class (see the other functions in QQmlEngine, like "qmlRegisterSingletonType":http://qt-project.org/doc/qt-5/qqmlengine.html#qmlRegisterSingletonType-2 etc.)

    There are other ways to expose c++ functions to QML, but again if you only have a simple function like this that might be overkill!? If you want to know more read "Integrating QML and C++":http://qt-project.org/doc/qt-5/qtqml-cppintegration-topic.html.

    Anyway your c++ function above looks a bit weird, you don't use QString (is that a std:.string or what?) and what is the buffer for?



  • That is a std::string. The buffer is for storing the first letter of the user input string as it is deleted in the next line. (The buffer is also a string)

    Well I wanted to learn Qt and I've always planned to learn C++ as well. I had the impression when I got Qt Creator it was made in mind with combining C++ and Qt and I'm all for doing that, I've done a little bit of WPF + C# in visual studio before and that was much easier than this (about as simple as the example I showed above really)

    To tell the truth I wasn't even aware that javascript was intergrated into QML but I absolutely want to be using C++ (I was never JS' biggest fan either way) since C++ and Qt are specifically what I am trying to learn.

    So you're telling me I can't simply write C++ code and forward it to QML? I need to write specifically Qt Compatible version of C++ to make it work? As in QML can't read basic C++? That's.... not very cool.



  • Short answer: no you can not simply call c++ from QML.
    QML is based on JavaScript, so to call c++ code you need a bridge and conversion from JS to c++ and wise versa. Luckily Qt does most of the things for you, but have to tell the QML engine about your c++ code and what you want to exposo to JS.
    You can't compare that to WPF because a xaml file is just a layout description that gets read in C#, if you want to use pure c++ there is no way you can use Qt, even if you use the clasic widget based Qt apps you need QObject classes to use signals and slots (dynamic events) or warpper classes.



  • How do I learn this Qt version of C++? Is it a lot of trouble?



  • You can start by reading the getting started guide if you click on documentation above, of course there are also many books. But if you are familiar with c++ you can also look at the many examples coming with the Qt installation.
    I think Qt is not that hard to learn if you know c++, it is like if you know c# you can't immediately develop WPF apps without understanding the basics of .NET and WPF, can you? ☺



  • Well that's the thing, it was easy as pie in WPF, no brainer really once you've got the syntax of WPF down (then again I wasn't making anything complicated)

    In other words yes I could practically immediately develop WPF apps just knowing C#, all I had to pick up was how to use WPF and that's pretty easy since its got a GUI designer interface like QT does (so you can learn the very basics without actually writing anything manually). All I had to figure out was how to link the C# code to the buttons really, I could develop in WPF without much effort knowing just C# (because really, picking up the syntax of things like WPF or QML, Javascript even doesn't take that much effort)

    The problem is I'm having a hard time finding documentation specifically aimed at teaching you how to write code from C++ to Qt Compatible C++ and then linking that to the QML.

    But it seems you linked me exactly that in your first post but it was just a lot more complicated (and a lot more text) than I expected it to be so I didn't quite see it the right way. Anyhow thanks, I'm gonna pick this up and then reconsider whether Qt still suits my purposes or not. :)



  • Actually it really is not much code you have to write to call your c++ function, as soon as you understand the basic concept of Qt and QML.
    if you want I can write a small example to show you how to do that. But not today I am writing from my phone and it is terrible to write code on a mobile device haha



  • I would appreciate that :)



  • Ok I think in your case this is the simplest way you ca do it:

    1. create a QObject class with your functions and properties, what you want to use from QML, I have created a class claled MyStuff containing a single function "toPigLatin" for this example:
      @
      class MyStuff : public QObject
      {
      Q_OBJECT
      public:
      explicit MyStuff(QObject *parent = 0) : QObject(parent) {}

      Q_INVOKABLE QString toPigLatin(const QString &s) {
      return s.mid(1) + s.left(1) + "ay";
      }
      };
      @
      you can simply call that function from QML like any other QML function, see below. The function need to be a Qt slot or use the Qt macro Q_INVOKABLE to make it accessible from QML or other scripting languages.

    2. in your main.cpp create an Object of the MyStuff class and register the object as the QML context object, there are other ways like I said in an earlier post but in your case this might be the simplest way:
      @
      QtQuick2ApplicationViewer viewer; // you should already have this in your main.cpp
      MyStuff myStuff;
      viewer.rootContext()->setContextObject(&myStuff);
      @
      that is all, now you can use the function toPigLatin like any global function from anyway in your QML project, e.g.:
      @
      Rectangle {
      width: 360
      height: 360

      MouseArea {
      anchors.fill: parent
      onClicked: console.log(toPigLatin("banana"))
      }
      }
      @
      that is just the hello world QML code, only changed the onClicked slot to a console log, as you can see it calls the c++ function toPigLatin there. :)

    In the c++ side of the function you can do whatever you like, keep in mind the input and output parameters to QML will be converted by the QML engine, so you should use compatible types like QString and not std:.string, that won't work here (unless you provide a custom conversion from and to JavaScript). If you need to use a std:.string internally you can just convert the QString to a std::string with QString::toStdString(), but in your case I think QString is the better choice anyways. :)


Log in to reply
 

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