[solved] connect to slot and specifying argument values
I have a QLineEdit object and I have connected the returnPressed() signal to a slot. The same slot though is called for another signal, and I'd like to differentiate between the slot caller using a bool argument. Unfortunately, I've been unable to call a slot and specify either true or false. I've tried like this:
connect(pEditObj, SIGNAL(returnPressed()), this, SLOT(onEditFinished(bool b=true)));
connect(pEditObj, SIGNAL(returnPressed()), this, SLOT(onEditFinished(true)));@
The first connect I've used seems to compile fine with no errors or warnings, but at runtime the slot function onEditFinished() is not called at all. The second connect I've used would not compile since the connection requires a function prototype, not a real function call.
Question is, is it possible to connect to a slot and spcify a slot argument value? I know this is possible when the incoming signal has parameters, and they will be passed through to the slot function, but in this case the returnPressed() signal has no parameters.
No, you can't. You could use a lambda if c++11 is an option.
Why do you have a slot call onEditFinished with a bool parameter ?
Tank you for your answer ;)
I have two objects (a few more coming as well) that should call the same onEditFinished slot because I have to do the same tasks; for instance I have a QPushButton that resets to the default value and a QLineEdit that should set a new one with its contents. After finishing they should do the same tasks, but I want to hide the QLineEdit if the call was from there. So basically I thought onEditFinished(true) means it comes from the QLineEdit object, onEditFinished(false) comes from wherever else.
Luckily, I just found the "QObject::sender()":http://qt-project.org/doc/qt-4.8/qobject.html#sender method in the docs and looks like this is something I could use. I never used it so I'm expecting trouble, but it should be fine for the task.
p.s. yes, C++11 is an option , could you show me the way you were thinking about?
sender() is the right and it works. You can set objectname using setObjectName to differentiate those objects in slot.
Do you mean hide the QLineEdit once it emitted returnPressed ?
If so, just connect returnPressed on hide and keep your slot clean
No, the slot actually does more things than hiding the QLineEdit object; if it was just about hiding it would have been too simple, wouldn't it? :-)
Indeed, but one never knows :-)