[SOLVED] Sending a argument to a slot
-
I understand that i am not able to pass a argument to a slot that was not a argument of the signal.
However this is exactly what i want to be able to achieve. and im sure there must be a work around if anyone can help?basically when a button is pressed i want to send some text through to my slot function:
e.g
@QObject::connect(button, SIGNAL(clicked()),this, SLOT(updateL("C:/")));@regards, Tim.
-
You can use C++11 lambdas :
@
QObject::connect(button, &QPushButton::clicked, [this] () {
updateL("C:/")
});
@ -
Or you use a [[doc:QSignalMapper]] object, especially if you're still on Qt4 or using a compiler that doesn't understand C++/11
-
Simply create a slot function without argument, connect your signal to this slot, in which your original slot get called as a normal function.
If you do not want to create a named function and your are using Qt5, you can connect your signal to the lambda function, in which you can call your original slot. [as kevlna65 suggested]
[quote author="Timmoth" date="1375433317"]I understand that i am not able to pass a argument to a slot that was not a argument of the signal.
However this is exactly what i want to be able to achieve. and im sure there must be a work around if anyone can help?basically when a button is pressed i want to send some text through to my slot function:
e.g
@QObject::connect(button, SIGNAL(clicked()),this, SLOT(updateL("C:/")));@regards, Tim.[/quote]
-
Thanks for both replys i think im going to go with Kevlna's method though since i am using Qt5.
@ QObject::connect(button, &QPushButton::clicked, this{Hi("C:/");});@
worked perfectly! -
However i have an issue now, when i try to pass a Qstring through i get an error:
error: C3493: 'path' cannot be implicitly captured because no default capture mode has been specified
@
QString path = d.absoluteFilePath();
QObject::connect(button, &QPushButton::clicked, this{Hi(path);});
@
regards, TimEdit: please use @ tags around code sections; Andre
-
Use this instead :
@
QObject::connect(button, &QPushButton::clicked, [this] () {// You're inside the lambda, you can write almoste everything here. QString path = d.absoluteFilePath(); Hi(path);
});@
-
i understand it a bit better now thanks Kevlna but im still getting the error message:
error: C3493: ‘path’ cannot be implicitly captured because no default capture mode has been specified
-
As d is a local variable you have tell the lambda how to capture it:
@
(1) auto l = &d { // only d is captured by reference };
(2) auto l = d { // only d is captured by value };
(3) auto l = & { // all local variables are captured by reference. };
(4) auto l = = { // all local variables are captured by value. (A copy will be made!) };
@There are several ways to combine different capture methods, refer to a c++ book of your choice for more details. (E.g. The C++ Programming Language 4, 11.4.3, page 293).
-
ok thanks alot PeerS! ill look into capture methods.
-
-