answered
2015-03-20 02:52:34 +0200
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