4

I'm trying to figure out how Qt and Qml work together, because I'm confused about where to use C++ and where to use JavaScript.

Let's say I have a couple of QML objects (like forms, inputs, dropdowns, etc.). Now, obviously these components have some logic code. Should I write this logic code in JavaScript or in C++?

Let's say my input has a group of properties and signals. Where and how should these be coded?

If I should write it in JavaScript, then how is C++ used. How are C++ and JavaScript connected?

4
  • 1
    Try to do everything with qml, if you can not do it with qml then just use C ++ Commented Nov 14, 2017 at 13:35
  • QML, in most cases is self-sufficient so as @eyllanesc already said you should not use C++ until you need some missing functionality. Take C++ as a way to extend QML. Here is good starting point to learn QML. Commented Nov 14, 2017 at 13:56
  • A simple use case would be to see the C++ side as "backend" and QML front-end code. So I'd do database connections and stuff in C++ and UI interactions with QML/JS, in simple terms. Commented Nov 14, 2017 at 14:19
  • Divide your Software in the three layers: Model - ViewModel - View. Keep the Model in what ever language. Write the ViewModel in C++ interfacing to the Model and providing a Qt/Q_PROPERTY-Interface to the View. Write the View in QML. Strive to write the view as declaratively as possible. Use C++ to write QML components that have a lot o logic, that you then instantiate in QML. If that is difficult, just write the logic part of a QML component in C++ as a QObject, instantiate it and bind. Commented Nov 14, 2017 at 15:41

1 Answer 1

4

The QML language was invented to be used to describe interfaces. It is meant to be simple to understand for designers.

This mean that in a program you will generally have all the logic implemented in C++ and the UI implemented in QML. To make the link between C++ and QML it is necessary to have some C++ code exposed to QML. There a many way to do that. For instance you could make a C++ class available in QML (see http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html), just make the instance of a singleton available in QML or inject a QObject pointer into the QML environment. All of this heavily use the Qt meta object system (http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html).

// in C++
class MyClass
{
    Q_OBJECT
public slots:
    int doSomething();
   ...
};

int main()
{
...
   engine->rootContext()->setContextProperty("foo", new MyClass());
...
}

// in QML

function foobar() {
    var anInt = foo.doSomething();
}

QML allowing you to write javascript you can also write a complete program without ever using C++, implementing everything in javascript. But this is generally a bad idea, especially if you need performances.

Unlike some say, C++ is not here for extending QML. But QML is here to offer a simple interface to C++ objects, allowing developers and designers to create fancy UI without typing/learning C++.

My personal rule when writing QML is to go to C++ as soon as possible. Sure you can write simple functions in QML, but you have to keep them short and simple to leverage the full power of the optimizations of the underlying QML and JS engines. Keeping QML fast is so hard there is a full page in Qt documentation of what to think about when using QML (http://doc.qt.io/qt-5/qtquick-performance.html).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.