Hello,
I am connecting QML object signal to a slot like so:
It creates between the two objects.
But I want to create between them.
How can I achieve it? Or is there a code in QML engine that I can tweak?
Thank you.
Michal
Hello,
I am connecting QML object signal to a slot like so:
It creates between the two objects.
But I want to create between them.
How can I achieve it? Or is there a code in QML engine that I can tweak?
Thank you.
Michal
Last edited by mfojtak; 14th February 2013 at 16:40.
Your question is ambiguous! can you make a simple example?
The C++ function is obviously an option. But it would not allow me to define the connections declaratively as with "onSignal: codeToExecute".
I need the direct connections for what they are exactly for - connecting slots directly. I developed a real-time application which is performance critical. The queued connections bring an overhead with copying slot arguments, queue slot arguments.
Also, I need the slot to run under the same thread as signal owner.
My application is a kind of dataflow/workflow application. Each module/building block is represented by a QObject which are interconnected via signals/slots.
I've created an XML configuration file for it. However, it would be much better to utilize QML, which is declarative, it allows me to embed a Javacript code - well you know benefits of QML :-)
Of course you can define connections in a declarative manner. It's just a matter of exposing a proper interface from C++ to QML.
Khem... QtQuick is not really designed for time critical tasks. Queued connections are the least of your problems.I need the direct connections for what they are exactly for - connecting slots directly. I developed a real-time application which is performance critical. The queued connections bring an overhead with copying slot arguments, queue slot arguments.
I'm not even going to explain now what that is Wrong(TM).Also, I need the slot to run under the same thread as signal owner.
QML (in Qt5) can be used without QtQuick as a way to define relations between objects. However its work ends after the QML tree is parsed and all functionality is embedded in the object types you expose from C++. You can expose an alternative for Connections that will make direct connections however be aware that writing slots directly in QML scripts has its limitations. Because they are executed by the script engine, they are limited to a single thread (as the script engine itself cannot be called from multiple threads). If there are objects behind the scene that live in other threads, trying to invoke script code from such thread will simply crash your program.My application is a kind of dataflow/workflow application. Each module/building block is represented by a QObject which are interconnected via signals/slots.
I've created an XML configuration file for it. However, it would be much better to utilize QML, which is declarative, it allows me to embed a Javacript code - well you know benefits of QML :-)
Either way, using JavaScript with garbage collecting, JIT compiling and all that stuff for time critical tasks is very likely bad design.
QtQuick is just a QML plugin. I want to use QML as a language to define an object tree of any QObjects. The app could be a deamon with no UI.Khem... QtQuick is not really designed for time critical tasks. Queued connections are the least of your problems.
What do you mean? That's what direct connections are for - slot is executed by the same thread which emits the signal.I'm not even going to explain now what that is Wrong(TM).
Direct connections are used when necessary. The UI elements are connected with other parts by queued connections which makes sense because I don't want my UI to disrupt my real time part (UI runs under dedicated thread anyway).
How is that possible?You can expose an alternative for Connections that will make direct connections
The application doesn't contain only time critical parts. Javascript gives user an extra flexibility. Let say I need to add some extra logging to my app. I can just write handler which will do logging in QML without recompiling my app. I am fully aware that Javascript handlers run under dedicated JS engine's thread.Either way, using JavaScript with garbage collecting, JIT compiling and all that stuff for time critical tasks is very likely bad design.
I just need to have a choice which connection type to use. Real-world applications usually need both.
If your objects live in different threads then having direct connections between them is a good first step to the end of the world.
Qt does not bring any real-time guarantees. Regardless if you use direct connections or queued ones.I don't want my UI to disrupt my real time part (UI runs under dedicated thread anyway).
It's as possible as exposing any other QML element. "Connections" is by no means different.How is that possible?
And that logging has influence on when your time-critical part gets executed (or not).The application doesn't contain only time critical parts. Javascript gives user an extra flexibility. Let say I need to add some extra logging to my app. I can just write handler which will do logging in QML without recompiling my app. I am fully aware that Javascript handlers run under dedicated JS engine's thread.
You do have that freedom. You just need to enable it for yourself.I just need to have a choice which connection type to use. Real-world applications usually need both.
The objects which I connect directly live in the same thread. But the code is executed by a new QThread that I create. This thread acts as a realtime timer which triggers the execution of slots which emit signals and other slots and so. This object graph is defined by a custom XML at the moment. Take a look at the project's (beta) website. It might help you to understand what I am after.If your objects live in different threads then having direct connections between them is a good first step to the end of the world.
Qt is only an application framework. The OS does bring real-time guarantees. I am using clock_nanosleep() kernel instruction in my C++/Qt timer code. It works great. My realtime part is not missing deadlines on sub-microsecond basis - but only if my modules (QObjects) are connected directly. With queued connections it doesn't even make sense to measure delays as you bring an extra queue in between my modules. That's why I need direct connections.Qt does not bring any real-time guarantees. Regardless if you use direct connections or queued ones.
The Connections object is used as this. See the following example:It's as possible as exposing any other QML element. "Connections" is by no means different.
Looking at the code above I can't see any way how to impact the connection type of clicked signal. I must be missing something. Should I inherit Connections class? I don't have problem with exposing a QML element. My problem is that the notation onSignal:slot always creates queued connection.Qt Code:
Connections { target: area onClicked: foo(parameters) }To copy to clipboard, switch view to plain text mode
OK, let say I have implemented my DirectConnections object. Now I also have object called Sender which emits signal called signal. And I have object called Receiver with slot called slot(). All three objects are exposed by my QML plugin called CustomPlugin.
The QML would look like this:
Qt Code:
import CustomPlugin 1.0 Sender { id: sender } Receiver { id: receiver } DirectConnections { target: receiver NOW HOW DO I CREATE CONNECTION BETWEEN onSignal and receiver.slot() }To copy to clipboard, switch view to plain text mode
The question is hidden in the code above. What shloud I do to implement such a class which would understand this: onSignal: receiver.slot() ?
I know you can do it with Connections object. But this object is part of QML language which parses "signal: slot" notation in a special way.
It would be brilliant if I could have custom object which keeps the same notation as Connection object. But how to implement it. Could you provide few lines of code which would illustrate it?
For example:
javascript Code:
DirectConnections { source: sender.signal target: receiver.slot }To copy to clipboard, switch view to plain text mode
I'm not sure how to access the signal and the slot itself from the C++ side. If you can't find a way to do it, you can always use separate properties for the signal and slot signatures (as strings) that you will then use in the backend.
More likely having two properties of type QObject* and two properties of type QString.
Once all four properties are set, code is triggered that does
1) lookup of slot and signal through QMetaObject
2) call connect with the two objects and the retrieved signal and slot signatures
Cheers,
_
Added after 8 minutes:
Ok, I have to say I don't understand the problem.
I get DirectConnection behavior in a simple test application
Qt Code:To copy to clipboard, switch view to plain text mode
Qt Code:
#include "timer.h" #include <QDebug> #include <QTimer> { m_timer->setInterval( 1000 ); connect(m_timer, SIGNAL(timeout()), this, SLOT(onInternalTimeout())); m_timer->start(); } void Timer::onInternalTimeout() { qDebug() << Q_FUNC_INFO; emit timeout(); qDebug() << "emit timeout() returned"; }To copy to clipboard, switch view to plain text mode
Qt Code:
import QtQuick 2.0 import CustomComponents 1.0 Timer { id: timer onTimeout: console.log( "onTimeout in QML" ) }To copy to clipboard, switch view to plain text mode
Obviously exporting the Timer class with qmlRegisterType in "CustomComponents" version 1.0
Output is
Qt Code:
void Timer::onInternalTimeout() onTimeout in QML emit timeout() returnedTo copy to clipboard, switch view to plain text mode
So emit returns after the "QML slot" has been called. AKA DirectConnection
Cheers,
_
Last edited by anda_skoa; 12th March 2013 at 18:58.
Yes, that's what I mean in the last paragraph of my previous post.
I think OP wants a direct connection between objects living in different threads which is a Bad Thing.Ok, I have to say I don't understand the problem.
I get DirectConnection behavior in a simple test application
That makes sense and I will give it a try.More likely having two properties of type QObject* and two properties of type QString.
Once all four properties are set, code is triggered that does
1) lookup of slot and signal through QMetaObject
2) call connect with the two objects and the retrieved signal and slot signatures
As I stated before - all objects live in the same thread and signals are emitted/slots executed by the same thread.I think OP wants a direct connection between objects living in different threads which is a Bad Thing.
Added after 1 30 minutes:
So I implemented DirectConnection class and it works.
Here is the header file:
Qt Code:To copy to clipboard, switch view to plain text mode
Source file:
Qt Code:
#include "directconnection.h" #include <QMetaMethod> { } { return _source; } { _source = source; tryConnect(); } { return _destination; } { _destination = destination; tryConnect(); } { return _signal; } { _signal = signal; tryConnect(); } { return _slot; } { _slot = slot; tryConnect(); } { int index = metaObject->indexOfMethod(signature.toUtf8().constData()); return metaObject->method(index); } void DirectConnection::tryConnect() { if(_source && _destination && !_signal.isNull() && !_slot.isNull()) { _destination, slot, Qt::DirectConnection); } }To copy to clipboard, switch view to plain text mode
Thank you guys!
Last edited by mfojtak; 13th March 2013 at 11:20.
Bookmarks