Is there a way to find the total number of signals/slots connections in a Qt application Someone refer me to the gamma ray, but is there any easier solution?
Asked
Active
Viewed 1,165 times
2
-
1You are talking about signals/slots connections, right? I adjusted the question because "connection" could be database or network as well. Do you want to count Qt internal connections as well, or only the ones from your own objects? What do you need the information for? There might be a better solution. Does this help? http://stackoverflow.com/questions/2755694/determine-signals-connected-to-a-given-slot-in-qt – Simon Warta Oct 06 '15 at 19:03
-
Thanks for your comments and editing, I am actually looking for a method to check and exam whether there are duplicated connections, like some times the same signal/slot pair is connected multiple times. – Nyaruko Oct 06 '15 at 19:07
-
2@Nyaruko not an answer on your question, but if you want to avoid duplicates, you may use `Qt::UniqueConnection` flag. Btw, GammaRay is open-source, you may research it code. It's very powerful tool. – Dmitry Sazonov Oct 06 '15 at 19:12
-
1Other way - is to manually track each connection. `QObject::connection` returns `QMetaObject::Connection` object. You may write a wrapper class, that will store this info and will be attached to your objects. – Dmitry Sazonov Oct 06 '15 at 19:15
1 Answers
2
Check Qt::UniqueConnection
.
This is a flag that can be combined with any one of the above connection types, using a bitwise OR. When Qt::UniqueConnection is set, QObject::connect() will fail if the connection already exists (i.e. if the same signal is already connected to the same slot for the same pair of objects). [...]
Then use an assertion in case the connection did already exist, which will crash your program indicating a programming error:
QLabel *label = new QLabel;
QLineEdit *lineEdit = new QLineEdit;
auto ok = QObject::connect(lineEdit, &QLineEdit::textChanged,
label, &QLabel::setText,
Qt::UniqueConnection);
Q_ASSERT(ok);
Disclaimer: untested.

Simon Warta
- 10,850
- 5
- 40
- 78
-
`bool ok = QObject::connect` is wrong. Return type is `QMetaObject::Connection` - it may be used for future diagnostics. – Dmitry Sazonov Oct 06 '15 at 19:14
-
Anyway, your solution is not an answer, because you can't count number of existing connections. – Dmitry Sazonov Oct 06 '15 at 19:16
-
Isn't this it about solving the underlying problem the most elegant way and helping the guy who has not been able to express his issue without getting downvoted twice? Feel free to offer a better solution and I'll be happy to review and push it. – Simon Warta Oct 06 '15 at 19:21
-
So we should ask @Nyaruko about underlying problem, instead of using telepathy. Btw, I didn't downvoted. – Dmitry Sazonov Oct 07 '15 at 07:19
-
1@Dmitry, the `bool ok = QOjbect::connect` uses the bool cast operator for QMetaObject::Connection to determine if the connection is valid. – pixelgrease Oct 30 '21 at 23:15
-