Solved Use of onCompleted
-
I am a novice at QT and trying to understand what someone from another state has done. An example is the use of: OnCompleted.
From: https://doc.qt.io/qt-5.10/qml-qtqml-component.html
Section: completed()
The text states: Emitted after the object has been instantiated.
Question: Emitted to what? Something must catch it in order to be useful.
Should there be something like:
Item
{
Id:item_36
…
Compononent.onCompleted: { … }
… }
And somewhere else there is something like:
wait_for( item_36.OnCompleted ) -
emitted - if nothing is connected to it - nothing
You'll want to be reading this:
http://doc.qt.io/qt-5/signalsandslots.html -
@BKBK said in Use of onCompleted:
Should there be something like:
Item
{
Id:item_36
…
Compononent.onCompleted: { … }
… }
And somewhere else there is something like:
wait_for( item_36.OnCompleted )The
wait_for
part is not needed. Following the example in your link:Rectangle { Component.onCompleted: console.log("Completed Running!") }
As soon as the
Rectangle
finishes loading, it emits thecompleted
signal. This triggers theonCompleted
signal handler, which runs this JavaScript code:console.log("Completed Running!")
-
@6thC
In that signals and slots page referenced by 6thC, in the third code block, it defines an object named Counter with a function SetValue() and one argument. If the new value is different it will emit signal: valueChange( value )In the next block the code instantiates two objects of type Counter, a and b.
Skipping the connect part I see that method setValue() is called for object a with value 12 and the same goes for object b and value 48.
Backing up to that second line of code there are two parts I do not get. I searched but did not find any discussion of use of &. Does this follow the C language and, in this use, the arguments to function/method connect are received by address and opposed to received by value? If so, why does this code fragment require the address of the variable rather than its value?
I don’t know what that line of code beginning with QObject::connect(…) does.
I did use ctl-f and searched for “connect” on the page but did not recognize anything that explained the syntax/grammar for this statement. I searched other places within the QT web sites and did not find a section describing the various characters used by QT. I presume that QT assigns the same meanings as the C language.
-
@BKBK
On re-read of my post I should have put that entire line of code in my reply. I do not see an edit button so I post that line in this reply.QObject::connect(&a, &Counter::valueChanged,
&b, &Counter::setValue);Please explain what this does?
-
Have a look also at the examples in http://doc.qt.io/qt-5/signalsandslots-syntaxes.html
@BKBK said in Use of onCompleted:
@BKBK
On re-read of my post I should have put that entire line of code in my reply. I do not see an edit button so I post that line in this reply.Click the triple-dots at the bottom-right corner of your post. The Edit button is inside.
QObject::connect(&a, &Counter::valueChanged,
&b, &Counter::setValue);Please explain what this does?
It connects the
valueChanged()
signal ofa
instance to thesetValue()
slot ofb
.For example, when
a
's value is changed to 42, Qt automatically calls this behind-the-scenes:b->setValue(42);
@BKBK said in Use of onCompleted:
I presume that QT assigns the same meanings as the C language.
Qt is a C++ library.
-
Well, that link does explain... it's actually a really good explanation. What I can attempt will surely leave gaps but here goes:
&a is the reference to an object instance of Counter class type identified as
a
Counter has a functionvalueChanged
Connect takes x2 (sender && receiver objects) instance and a function signatures. Also a connection type argument.
So that line of code is connecting instance a's valueChanged function to instance b's setValue slot (probably, it maybe a signal, all we know is it has the same function signature as valueChanged: see new sig/slot syntax
-
@6thC said in Use of onCompleted:
What I can attempt will surely leave gaps but here goes:
&a is the reference to an object instance of Counter class type identified as
a
Counter has a functionvalueChanged
Connect takes x2 (sender && receiver objects) instance and a function signatures. Also a connection type argument.
So that line of code is connecting instance a's valueChanged function to instance b's setValue slot (probably, it maybe a signal, all we know is it has the same function signature as valueChanged: see new sig/slot syntax
Yep, that's right!
-
I am still missing something. Go back to that one line of code from the page on Signals & Slots.
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);If there was a, b, and a new item c could we write:
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue, &c, &Counter::setValue);
The function connect() had, what I view as two sets of arguments, with each set containing two items, the address of the object and the address of an integer that was changed.
In the original line of code those two arguments were repeated once for each item that could send the signal. The method connect() can associate N receivers. Just add a pair of arguments for each sender that is to be connected to the slot owned by the code that calls method connect().
Is that right.And just to be sure, in the line of code the argument: &a means that the address of object a is being provided to method connect. Is that correct.
-
@JKSH My Internet Explorer does not display the ellipsis (triple dots). I tried ctl-F and it says that .. (double dots) are not found on the page, much less triple. Looks like a problem with the government firewall.
In another forum question I tried using [CODE] some_function()[/CODE] and it was displayed as simple text. Is there another phrase I can use to specifically mark lines of code? -
Hi
Yes the & takes the address. When you see such in a connect then make sure the objects in question do not run out of scope.
likevoid SomeFunc() {
Widget a;
Widget b;
connect( &a, xxx, &b, yyy)
} // both a and b are deleted here and the signal will never fire.The connect works only for 2 objects.
The sender and the receiverbasically you are sying
connect( this object i point to, SIGNAL, other object i point to, SLOT )Also note its legal to connect a signal to a signal to create a forward.
Also multiple can be connected to same signal.
So with your case with new C.
you would hook it up A like b is. -
Hi,
The
</>
icon on the right of the editor icon bar inserts what is needed to create code blocks. -
@BKBK said in Use of onCompleted:
If there was a, b, and a new item c could we write:
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue, &c, &Counter::setValue);
The function connect() had, what I view as two sets of arguments, with each set containing two items, the address of the object and the address of an integer that was changed.
In the original line of code those two arguments were repeated once for each item that could send the signal. The method connect() can associate N receivers. Just add a pair of arguments for each sender that is to be connected to the slot owned by the code that calls method connect().
Is that right.No. You can only connect one sender to one receiver at a time.
See the documentation for QObject::connect() at https://doc.qt.io/qt-5/qobject.html#connect
And just to be sure, in the line of code the argument: &a means that the address of object a is being provided to method connect. Is that correct.
Yes.
@BKBK said in Use of onCompleted:
Internet Explorer
What version? Are you allowed to use a newer version or even a different browser?
does not display the ellipsis (triple dots). I tried ctl-F and it says that .. (double dots) are not found on the page
It's not searchable text. It's an image, a vertical stack of dots:
Looks like a problem with the government firewall.
If you can't see the dots, I'm guessing it's either your browser is too old, or your browser has an ad blocker or a tracker blocker
In another forum question I tried using [CODE] some_function()[/CODE] and it was displayed as simple text. Is there another phrase I can use to specifically mark lines of code?
Add 3 backticks (`) before, and 3 after your code.
-
Just to clarify:
A signal can be connected to many slots and signals. Many signals can be connected to one slot. If a signal is connected to several slots, the slots are activated in the same order in which the connections were made, when the signal is emitted.
I found personally to get the feel of signals and slots was to just use them.
Generally it's the pretty simple concept of:
single object instance (pointer or reference) and one of it's functions
connecting to another
single object instance (pointer or reference) and one of it's functionsSo you can't define multiple connections in the one connect - you can do as many connects as you need.
The main thing is the function type signatures need to match. You can use overloaded methods and even use lambdas as well so pretty much anything you need - it can happen.In your most recent question you'd just do:
QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue); QObject::connect(&a, &Counter::valueChanged, &c, &Counter::setValue);