[Solved] C++ Signals for QML Layer
-
Hi,
I have a question about the signal connection between my C++ logic layer and my QML visual Layer.
What I need is to connect my interface class with the QML layer, I found some tutorials about that topic.
The answer seems to be theqmlRegisterType<QmlInterface >("QmlInterface ", 1, 0, "Interface ");
statement.
Ok so far so good, but in this examples I'm missing one detail.Lets take the QmlInterface class. In the example the QmlInterface class is just one independent class with no connection
to other classes or the rest of the C++ logic.In my current project I have a QmlInterface class which is designed to send diffenrent events and informations to my QML layer.
This QmlInterface needs a pointer to my CoreEngine which is handling the different events. But I can't figure out how I can
use signals of my QmlInterface class in the QML layer.Lets say this is a pretty primitive variant of my QmlInterface class. I instantiate this class so far to set an index from a CoreEngine
when the index is changed I want to emit a Signal indexChanged() so that the QML layer know that the index was changed.This class should work for the qmlRegisterType approach.
But my QmlInterface needs a pointer to the CoreEngine.Until now the QmlInterface class has a parameter CoreEngine *core for the interaction with the core of my app.
But this isn't working when I use the qmlRegisterType().So what can I do to realize this connection from CoreEngine-> to QmlInterface -> to QML-layer ?
I hope this isn't to confusing and you nearly understand whats my issue.
@
class QmlInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(int index READ index NOTIFY indexChanged)public:
QmlInterface(QObject *parent=0) : QObject(parent)
{
/some stuff/
this->m_index = 1;
}int index() const { return this->index; } void changeIndex(int index) { this->m_index = index; emit indexChanged(); }
signals:
void indexChanged();private:
int m_index;
};@
-
Here are a couple things you could try:
- Make the CoreEngine available via a static function, and call that function in the QmlInterface constructor
@QmlInterface(QObject *parent=0) : QObject(parent)
{
m_coreEngine = CoreEngine::instance();
}@ - Use setContextProperty() to expose the object, rather than adding it as an element in the QML tree. See "http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#structured-data":http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#structured-data for more details on this approach. (you'd then need to use the Connections element to connect to signals.)
Michael
- Make the CoreEngine available via a static function, and call that function in the QmlInterface constructor
-
Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.
But I don't understand your idea with the static instance() method.
What should the instance() method return ? a pointer to the Core ?But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.
Maybe you can tell me what instance() should do in your opinion.
Jan
-
[quote author="Schneidi" date="1283522910"]Hi, I used setContextProperty() already but I didn't get access to the signals maybe I have done something wrong.[/quote]
How did you try to access the signals? Assuming you exposed your class as "myInterface", and it had a signal named "mySignal()", the following should work:
@Connections {
target: myInterface
onMySignal: ...
}@[quote author="Schneidi" date="1283522910"]But I don't understand your idea with the static instance() method.
What should the instance() method return ? a pointer to the Core ?But how can a static method return a pointer to its object ? As far as I know static methods have no specific object.
Maybe you can tell me what instance() should do in your opinion.[/quote]
Sorry, I should have been more explicit. I was thinking you could use the Singleton pattern, if it made sense in the context of your application. So something like:
@CoreInstance *CoreInstance::instance()
{
static CoreInstance *myInstance = 0;
if (!myInstance)
myInstance = new CoreInstance();
return myInstance;
}@Michael
-
Hi.
Like qml use QtSCript you can use connect function
@myObject.mySignal.connect(...)@
"http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections":http://doc.trolltech.com/4.6/scripting.html#signal-to-function-connections
Normally it's works.
[edit: fixed link / $chetankjain]