Singleton signal c++ qml example
-
It would be great if someone could give me an example of a working C++ singleton class with signals? and in qml a on...signal catch.
Idid register my singleton in the main() but it seems as I forgot something.
whilw running i get the message "Cannot assign to non-existent property "myApi"" -
Hi,
With register I guess you mean using qmlRegisterType. So once you register the C++ type, from QML you can instantiate that C++ type in QML and access those signals using their respective handlers. For eg If you have a signal mySignal() in C++ the corresponding handler that Qt automatically provides in QML is onMySignal.
Is this what you wanted to do ? Probably it would be better if you show some code. -
It was a miss spelling .. It works but there is no automatic code completion.
There is a red line under the import statement and the message
"QML module not found" -
How did you import ? and how did you register ?
-
I use the example code from Qt.. but I wrote myApi instead of MyApi
but there is still a red line underneath the
@
import Qt.example.qobjectSingleton 1.0
@
statementUsage:
// first, define your QObject which provides the functionality.
@
class SingletonTypeExample : public QObject
{
Q_OBJECT
Q_PROPERTY (int someProperty READ someProperty WRITE setSomeProperty NOTIFY somePropertyChanged)public:
SingletonTypeExample(QObject* parent = 0)
: QObject(parent), m_someProperty(0)
{
}~SingletonTypeExample() {} Q_INVOKABLE int doSomething() { setSomeProperty(5); return m_someProperty; } int someProperty() const { return m_someProperty; } void setSomeProperty(int val) { m_someProperty = val; emit somePropertyChanged(val); }
signals:
void somePropertyChanged(int newValue);private:
int m_someProperty;
};// second, define the singleton type provider function (callback).
static QObject *example_qobject_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)SingletonTypeExample *example = new SingletonTypeExample(); return example;
}
@
// third, register the singleton type provider with QML by calling this function in an initialization function.
@
...
qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
...
@
In order to use the registered singleton type in QML, you must import the singleton type.
@
import QtQuick 2.0
import Qt.example.qobjectSingleton 1.0
Item {
id: root
property int someValue: MyApi.somePropertyComponent.onCompleted: { someValue = MyApi.doSomething() }
}
@
Since singleton types do not have an associated QQmlContext object, then within the functions of a QObject-derived type that is registered as a singleton type implementation the QML context and engine information is not available. The QQmlEngine::contextForObject() function returns NULL when supplied with a pointer to an QObject that implements a singleton type. -
@
#include <QtQml>
#include "singletontypeexample.h"int main(int argc, char* argv[])
{
...
qmlRegisterSingletonType<SingletonTypeExample>("Qt.example.qobjectSingleton", 1, 0, "MyApi", example_qobject_singletontype_provider);
...@
-
Instead of
@
import Qt.example.qobjectSingleton 1.0
@try
@
import Qt.example.qobjectSingleton 1.0 as ExampleApi
property int someValue: ExampleApi.MyApi.someProperty
@From the docs.
-
Yes I can access the value .. "ExampleApi.MyApi.someProperty" there is no error message
But still a red line underneath the Import statement and no code completionis this maybe normal on Qt if I use pointers ?
-
This could be the parsing problems in QtCreator. Try restarting the QtCreator.
-
Hello.
I have exact the same problem.
At this time i have create a QtQuick Plugin project and register my singletontype in this project. In my other project i have add QML_IMPORT_PATH = PathToMyQtQuickPlugin and now i have no red line underneath the import statement in QML but i have no code completion.
When i run the project all works fine.
I am a little bit confused.
Have anybody a solution?
Thanks.
-
Hello.
I have exact the same problem.
At this time i have create a QtQuick Plugin project and register my singletontype in this project. In my other project i have add QML_IMPORT_PATH = PathToMyQtQuickPlugin and now i have no red line underneath the import statement in QML but i have no code completion.
When i run the project all works fine.
I am a little bit confused.
Have anybody a solution?
Thanks.
-
I still have no solution. A restart of Qt did not solve the problem
-
I still have no solution. A restart of Qt did not solve the problem
-
Hello.
I have seen the following video from Qt Developer Days 2013 about Qt Quick Controls.
https://www.youtube.com/watch?v=JLLIYysXsj8In this Video shows Jens Bache-Wiig live coding with Qt Quick Controls and bind controls to a c++ class. This c++ class has he registered as singleton and he has the same problem. He has no auto complete in Qt Creator.
I think at this time, we must life with this problem. I hope that this feature comes in Qt Creator in a future release.
@xenotrax: Thanks for reply.
-
Hello.
I have seen the following video from Qt Developer Days 2013 about Qt Quick Controls.
https://www.youtube.com/watch?v=JLLIYysXsj8In this Video shows Jens Bache-Wiig live coding with Qt Quick Controls and bind controls to a c++ class. This c++ class has he registered as singleton and he has the same problem. He has no auto complete in Qt Creator.
I think at this time, we must life with this problem. I hope that this feature comes in Qt Creator in a future release.
@xenotrax: Thanks for reply.