10

I'm using Qt 4.8.x so I don't have access to the new connect interface from Qt5, but I want to be alerted better when a signal/slot connection fails because I misspelled a signal or slot name.

Currently, all Qt does is spit out an error message when the connection is attempted. However, my program has a lot of output on stdout so it is easy to miss these errors sometimes. Is it possible to force my application to crash via assert, segfault, or other method, when a connect statement fails?

Cory Klein
  • 51,188
  • 43
  • 183
  • 243

2 Answers2

9

Yes: set the QT_FATAL_WARNINGS environment variable to a non-zero value.

You can do this during development in Qt Creator by going to the Projects pane, click Run, then under "Run Environment" click Details, then click Add.

Edit to add: you can of course also implement some wrappers for QObject::connect, that will check its return value and assert() if it returns false.

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
peppe
  • 21,934
  • 4
  • 55
  • 70
  • 1
    Can you be more specific on how to do this? – Cory Klein Jun 27 '13 at 20:53
  • I'll be more specific in my question. Is this an OS environment variable? Or is it something that you set in the `.pro` file? How do you set the `QT_FATAL_WARNINGS` environment variable to a non-zero value? – Cory Klein Jun 27 '13 at 20:57
  • It's an OS environment variable; how to set it depends on your OS/IDE. When using Creator, you can set additional environment variables in the Project pane, under the Run settings. Under Linux/Unix with a `sh`-like shell, you can run `QT_FATAL_WARNINGS=1 ./YourApp` or `setenv QT_FATAL_WARNINGS=1` to set it permanently in that shell. Under Windows it's somewhere in the Control Panel -> System. – peppe Jun 27 '13 at 20:59
  • This worked for me - add this line `qputenv("QT_FATAL_WARNINGS","1");` near the beginning of your `main()` function. – Harvey Nov 21 '21 at 08:17
4

I think at least one of the solutions in this question: Qt GUI app: warning if QObject::connect() failed? does what you need.

In particular, by using QErrorMessage::qtHandler you should be able to intercept those warnings and do whatever you want with them.

Community
  • 1
  • 1
CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31