Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How can you: application logic in C++ and presentation in QML
Forum Updated to NodeBB v4.3 + New Features

How can you: application logic in C++ and presentation in QML

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 3 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nvcnvn
    wrote on last edited by
    #1

    I just have some fun with QML and i love to use it to handle the UI, just UI!
    For me, I will define a new QML Elements in C++ with some method that handle the main (and maybe taking some time) application logic and use it at event handle for the presentation elements, like this:
    @Rectangle {
    width: 360
    height: 360
    CustomCppDefinedElements {
    id:foo
    }
    MouseArea {
    anchors.fill: parent
    onClicked: {
    foo.DoIt();
    }
    }
    }
    @
    But is there any better way?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Who knows? Depends on what you need and what the circumstances are. What I usually do is either:

      create a base C++ class inheriting from QQuickItem, add relevant methods and properties, register it and use in code to create children components

      or create plain QObject classes in C++ and export methods with Q_INVOKABLE (or slots) and properties with Q_PROPERTY, and then register it with QQuickView's root object

      I won't venture to say which is better - it depends on a concrete use case.

      (Z(:^

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chrisadams
        wrote on last edited by
        #3

        In Qt5 with QtQuick2 there are more ways to expose C++ functionality to QML.
        One particularly useful feature is the ability to register a QObject singleton type with the QML typesystem. You can call functions, bind to properties, etc, without having to explicitly instantiate an element or pass in a context property.

        See the "documentation":http://qt-project.org/doc/qt-5.0/qqmlengine.html#qmlRegisterSingletonType-2 for more information.

        In short, yes - QML was designed with C++ integration in mind. You should implement heavy application logic in C++, and implement only your UI with QtQuick, in many cases.

        Cheers,
        Chris.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved