Solved Can a signal call a non-slot method
-
@tomy said in Can a signal call a non-slot method:
Why QObject::connect method explained in Docs under the name Qt 5.12.2 still uses the Qt 4 synatx version for connections, please?
because it's still valid.
The new syntax has it's own entry, further down:
https://doc.qt.io/qt-5/qobject.html#connect-3Both are overloads of the QObject::connect call -> both get an entry in the docs ;-)
-
@tomy If you want to use new Qt connection syntax, signal and slot must have same signature.
In your example, signal has 2 QString parameters and slot only 1.. This ist not allowed!
Which of signal parameter should be used for the slot?You can do someting like this using lambda function:
connect(sender, &Sender::valueChanged, receiver, [=](QString str1, QString) { receiver->updateValue(str1); });
Hope this helps.
ps: using "reciever" as context, so connection will be deleted with reciever is deleted. This will avoid null pointer exceptions.
-
@J.Hilk
Thanks Mr. Hilk. :-)@KroMignon
Thanks. :) -
And I guess it's not yet possible to using a simple way like below connect a signal to two slots in one statement:
connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue1, &Receiver::updateValue2 );
And we still have to use two lines:
connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue1); connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue2);
Right?
-
@tomy right
-
@tomy nope, you will have to use 2 lines
or a lambdaconnect(sender, &Sender::valueChanged, receiver, [receiver] (QVariant argument)->void{receiver->updateValue1(argument); receiver-> updateValue2(argument);});
-
Well, you can use a lambda and call each method one after the other.
-
Thanks to all.
@J.HilkI used this:
connect(someAction, &QAction::triggered, this, [this]()->void { this->slot_1(); this->close(); });
The return type of
slot_1
isvoid
but that forclose()
isbool
, but since the return value of a slot is ignored when it's called by a signal in connections, so I also usedvoid
for the lambda expression above. -
@tomy seems about right.
You could technically omit the return type here, but proper form (strongly) suggest that you write one ;-) -
@J.Hilk
You mean this "->void
" part?
And that's once again because it's within a connection, right? -
@tomy said in Can a signal call a non-slot method:
You mean this "->void" part?
yes
And that's once again because it's within a connection, right?
no, the compiler can and will deduce the return type. However if you write
-> void { return true;}
you'll get a compile time warning/compiler error.