We have moved to a new Sailfish OS Forum. Please start new discussions there.
3

QML to C++ and C++ to QML

asked 2015-03-10 01:56:41 +0200

Matl gravatar image

updated 2015-03-10 09:21:32 +0200

I am searching some information about how to connect qml and c++ In general I worked with c++ in the past and got some based knowledge about it but I am not able to connect some buttons and so on with an c++ class or something like that.

My plan is to write a small Jolla app. For the beginning just a small note app where you can enter some text and give it to c++. Later on I want to integrate opencv and work with some image processing functions.

Are there any based information about qml to c++ for dummys? ;-) Or do you know some tutorials how to create a SailfishOS app with c++ ? Thanks!

edit retag flag offensive close delete

6 Answers

Sort by » oldest newest most voted
1

answered 2015-03-10 11:02:23 +0200

mikelima gravatar image

You may start here:

http://doc.qt.io/qt-5/gettingstartedqml.html#extending-qml-using-qt-c

For handling communication between the C++ part and the QML part, this may be useful as well.

http://doc.qt.io/qt-5/qtqml-syntax-signals.html

In general, you may want to have the business loginc in C++, and maybe you will need to write some custom component for special purpose application.

For a note application, you could have a TextNoteModel class and simply react to the signals from the qml components.

Anyway, the links I provided should get you started...

edit flag offensive delete publish link more
0

answered 2015-03-10 02:12:46 +0200

Claudio Maradonna gravatar image

The right way is to create business logic in C++ structure and create some objects that are registered in the QML space to call this logic. C++ don t nees to know how QML works because is not extensible in future. C++ <- QML But no C++ -> QML You can find a lot with Qt docs. Happy coding .

edit flag offensive delete publish link more
0

answered 2015-03-10 10:51:19 +0200

tortoisedoc gravatar image

updated 2015-03-10 10:52:15 +0200

SailfishsOS is basic linux. Meaning : any C/C++ application can run on it, once compiled.

That said, the main (and only) officially supported MVC framework of SailfishOS is Qt.

Qt's way to handle UI at the moment is QML; this is your only choice if you go with Qt. But it's a good one (see previous answer).

On top of QML, we have Silica QML library components ((C) jolla), which gives the system the look-and-feel.

You can, in theory, create your own UI - but you will need to do so from the scratch; and it will definitely not have the correct look and feel (unless you give it).

edit flag offensive delete publish link more

Comments

2

Qt's way to handle UI at the moment is QML

Qt was, in fact, made for desktops, not mobile platforms. And on desktops the Qt's way to handle UI is QtWidgets, which work an order of magnitude faster than QML since they are 100% C++. The fact that Jolla does not support pure C++ applications is one of the most sad things for me.

ScumCoder ( 2015-03-10 11:08:55 +0200 )edit
3

Who says you cant get widgets to work on Jolla

Also pure C++ apps are supported - in fact gcc is available. Of course using C++ and low-level api's (lower than qt, that is) will nuke your chance to get into harbour. But there is still openrepos.

tortoisedoc ( 2015-03-10 11:10:10 +0200 )edit
0

answered 2015-03-10 11:42:25 +0200

rcg gravatar image

Quite some time ago, I wrote a summarized blog post about "Exchange Data and Objects between C++ and QML and vice versa": http://ruedigergad.com/2011/11/13/exchange-data-and-objects-between-c-and-qml-and-vice-versa/

You may also be interested in the "QML Duck-typing Example": http://ruedigergad.com/2012/07/26/qml-duck-typing-example/

hth

edit flag offensive delete publish link more
0

answered 2015-03-10 11:51:49 +0200

MikErk gravatar image

Hey,

I requested this already some time ago: https://together.jolla.com/question/85024/sailfish-sdk-requesting-well-documented-c-and-qml-tutorial/

Have a look at Michals Code, I understand now more of it then before

edit flag offensive delete publish link more
0

answered 2015-03-20 02:52:34 +0200

Matl gravatar image

Hey guys, first of all thank you very much for your answers :)
I read many pages but I have still a problem with linking a C++ function/class with QML. I will post some of my code and the error - I think there is not very much missing and maybe you could help me with a few hints:

I have a class which is called "meineklasse.cpp"

#include "meineklasse.h"
#include <QDebug>
meineklasse::meineklasse(QObject
*parent) : QObject(parent) { qDebug() << "... meineklasse ... constructor";}
meineklasse::~meineklasse() { qDebug() << "... meineklasse ... destructor"; }
void meineklasse::meinefunktion() { qDebug() << "... meinefunktion"; }

With the header "meineklasse.h"

#ifndef MEINEKLASSE_H
#define MEINEKLASSE_H
#include <QObject>
class meineklasse : public QObject {
Q_OBJECT public:
explicit meineklasse(QObject *parent=0);
virtual ~meineklasse(); public slots:
void meinefunktion(); private: };
#endif // MEINEKLASSE_H

Now I am registering the C++ function in the main programm:

#ifdef QT_QML_DEBUG
#include <QtQuick>
#endif
#include <sailfishapp.h>
#include "meineklasse.h"
int main(int argc, char *argv[])
{
qmlRegisterType<meineklasse>("harbour.meineapp.meineklasse", 1, 0, "meineklasseqml");
return SailfishApp::main(argc, argv);
}

In the QML file "SecondPage.qml" I want to execute the function "meinefunktion" which I declared in the class "meineklasse":

import QtQuick 2.0
import Sailfish.Silica 1.0
import harbour.meineapp.meineklasse 1.0
Page {
    id: page
    SilicaListView {
        id: listView
        model: 20
        anchors.fill: parent
        header: PageHeader {
            title: qsTr("Nested Page")
        }
        delegate: BackgroundItem {
            id: delegate
            Label {
                x: Theme.paddingLarge
                text: qsTr("Item") + " " + index
                anchors.verticalCenter: parent.verticalCenter
                color: delegate.highlighted ? Theme.highlightColor : Theme.primaryColor
            }
            onClicked: {
                console.log("Clicked " + index)
                meineklasseqml.meinefunktion()
            }
        }
        VerticalScrollDecorator {}
    }
}

After compiling an executing the app in the virtual machine I can switch to the second page and click on a button, after that I get the debug message that the button is clicked "[D] onClicked:55 - Clicked 4" but also the error
"[W] unknown:57 - file:///opt/sdk/FirstApp02/usr/share/FirstApp02/qml/pages/SecondPage.qml:57: ReferenceError: meineklasseqml is not defined" So what is the problem? I thought I would define the C++ class against QML with the argument "qmlRegisterType<meineklasse>("harbour.meineapp.meineklasse", 1, 0, "meineklasseqml");" what or how can I register and open a C++ function in QML?</meineklasse>

Thanks for your answers. regards matl

edit flag offensive delete publish link more

Comments

(OMG code in german :P ) You should write that kind of questions on the devel ML.

Personally, I do the following in the main class:

DataProvider* dataProvider = new DataProvider();
view->rootContext()->setContextProperty("dataProvider", dataProvider);

Then dataProvider is accessible in QML.

Sthocs ( 2015-03-20 13:36:47 +0200 )edit

Just a little bit german code, because at the moment nothing is working and I have to understand the basics of QML in connection with C++. So I do not understand your code, thought I have to do it with "qmlRegisterType". The problem I am fighting is how to call that function or class in QML.
Furthermore after I register the class with "qmlRegisterType" the class is from QML shown and there is no error while compiling. But I can I finally call a function... that can not be that difficult ^^

Matl ( 2015-03-20 13:46:35 +0200 )edit

You only registered your qml data type. Thats like a class declaration in C++. In order to call functions you must instanciate your type as an actual object. You can do that in C++ (like in Sthocs post) or in QML .

You really should look at the Qt5 doc, there are very good examples for that stuff.

mrbluesky ( 2015-03-20 15:49:08 +0200 )edit

What I was saying is that instead of the line qmlRegisterType<meineklasse>..., I directly instantiate my class, and expose it to QML with setContextProperty.

Then in QML I can do dataProvider.someMethod()

If you want to instantiate it in QML, you can look at the answers here for an example. Or read more about QML Component as mrbluesky says.

Sthocs ( 2015-03-20 18:01:48 +0200 )edit
Login/Signup to Answer

Question tools

Follow
6 followers

Stats

Asked: 2015-03-10 01:56:41 +0200

Seen: 8,360 times

Last updated: Mar 20 '15