View Full Version : Slot with a return value?

20th December 2009, 12:59

May I make a slot with a return value, like:

public slots:
bool saveAs();
connect(savePB, SIGNAL(clicked()), this, SLOT(saveAs());

Practically, it works, but is this documented in Qt or allowed?

20th December 2009, 13:08
Since slots are normal C++ functions you can!

20th December 2009, 15:41
The return value of a slot is only retrieved if you call it like a regular function.

20th December 2009, 16:22
Thank you for your answers! I know that practically it is allowed. I even saw the moc_file.cpp code and made an idea of what this is like. But what I searched for was a Qt documentation about that. Which I searched also on the internet and in some books, but could not find it. I only found a sample program on the internet, which seemed to be from Qt officials, and it contained a non-void slot, and the author said is "non-standard" or something like that.

It is very easy to skip this problem: make a void slot which calls the non-void function. I don't want to take any risks in my software, so that is why I did this trick.

20th December 2009, 17:14
But there is no risk. In Qt documentation it is said that (almost - I think method with variable parameters count - '...' can't) every member function can be a slot. Just place it in public/private/protected slots section in class declaration. So making void returning slots to call non-void returning methods is waste of one more function call, of course unless you don't need returned value.

20th December 2009, 17:20
It's non-standard because a non-void slot will have it's return code ignored. If you wish to call that function normally too, then it makes sense. It's better than having a void slot calling a non-void slot when you are going to ignore the return value anyway - thats just a waste.

20th December 2009, 17:40
Detailed explanation: I have the function bool fileSave(), which saves the file, and returns true or false (success or not). The user can press button "Save", which is connected to this fileSave. For this, I don't need the bool return value. But I need it for this: when the user wants to Quit my application, I show him a question: "Do you want to save current file". He says "Yes". Then, he may save or hit cancel in the Save file dialog. I need the return code of fileSave(), if it is false, I don't close my application (closeEvent->ignore()).

Yes, you are right that there is an additional function call if I implement a slot void saveFileSlot(), which calls bool fileSave() and ignores the bool return. But remember the 10-90 rule (I mean, you need to only optimize critical code). With current speed, it really does not matter an additional function call for a button press.

I searched the Qt sources and could not find a non-void slot.

Yes, I know current code works. But there is also some other risk: this type of code (non-void slot) may not be tested thoroughly by Qt team.

20th December 2009, 18:27
In the end, its upto you. It's your application.

However, since we know how slots are called, there is really no risk to having a non-void slot.

20th December 2009, 19:15
I sometimes use slots that return values in my code. It's nothing special. As for non-void slots in Qt - I'm almost sure there are some. But they are called using QMetaObject::invokeMethod() because their value is interesting to the caller so it needs to be retrieved.

21st December 2009, 07:27
OK, thank you, I'll think about it.