I did not try that. Note that it doesn't remove the signal or the slot, it just disconnects the connection between the two.
I would not call it a great implementation either. It is a bit of a trick, and it is not a cheap trick to pull off (creating a whole new QObject instance to just monitor a single signal-slot connection). It could be optimized of course to only ever use a single QObject instance to do the management of potentially multiple single-shot connections. But that was more work :)
Note that with Qt 5, the implementation would be a bit different and cheaper. Because Qt 5 returns a QMetaObject::Connection instance when you call connect, you can just store that connection instance and use that to do your disconnect. If you have C++/11, you can also use a lambda to pull off this trick even cheaper. Something like:
@
//this is untested, of course
inline singleShotConnect(const QObject * sender,
const char * signal,
const QObject * receiver,
const char * method)
{
auto connection = QObject::connect(sender,
signal,
receiver,
method);
if (connection) {
//only disconnect if the connection worked
QObject::connect(sender, signal,
connection {
QObject::disconnect(connection);
};
}
}
@
Of course, you'd probably want to provide all the connect overloads. Perhaps you have to that manually, perhaps with a bit of template magic you can do it in one go. I do think that it would be nicer to just have a connection type singleShot, but a work-around like this would work as well.
Edit: I have added a "suggestion":https://bugreports.qt.io/browse/QTBUG-44219 to add this to Qt.