1

Windows 7 SP1
MSVS 2010
Qt libraries 4.8.4 for Windows (VS 2010)
Visual Studio Add-in 1.1.11 for Qt4

I, at first, couldn't figure out why this slot didn't fire:

connect (lineEdit, SIGNAL(textChanged(const QString &)),
         this, SLOT(enableFindButton(const Qstring &)));

A diff made it clear: Qstring should be QString.

My question: Why did it compile? In fact, it will compile with this:

connect (lineEdit, SIGNAL(textChanged(const nonsense &)),
         this, SLOT(enableFindButton(const more_nonsense &)));

Is this expected behavior? Why wouldn't this raise an error?

Macbeth's Enigma
  • 375
  • 3
  • 15
  • http://stackoverflow.com/questions/5880763/qt-signal-slot-macro-declaration?lq=1 (among others) – Brian Roach Dec 29 '12 at 00:50
  • 2
    This is simply because MOC (meta object compiler) isn't smart enough to deduce types during compile time, but Qt should generate some kind of warning message during runtime that signal can not be connected with slot. – mip Dec 29 '12 at 01:10

1 Answers1

6

As far as I know and understand how works Qt, the connection lines works on runtime, not in compilation time. This means that all the operations needed to connect a signal and a slot are performed when the code flow reach that part.

Remember something important about Qt, some of the calls are only macros, not C++ functions. For example, the line "Q_OBJECT" you should add in your class declarations to enable the signals and slots mechanims works, is a macro. Also, the connect call is a macro (...), emit is a macro, etc. Those calls expand into real code once MOC (a pre-compiler, translates Qt macros into real code) analize your code.

Also, the signal/slot mechanism, I repeat, as far as I know, works on runtime, not compilation time. If you read the documentation of the connect macro, it says that the "SIGNAL" and "SLOT" macros you place there, convert the stuff you put there into a string, obviously, with some kind of format, that maybe is too complex to used by hand, so, since it is a string working there, the compilation can't check if the string is correct or not, that is checked on runtime.

I hope my bad english let you understand me, and I hope my knowledge is big enough to not being saying (too much) incorrect tinghs.

  • 2
    While Q_OBJECT macro expands into QMetaObject MOC seems to ignore SIGNAL/SLOT macros completely. Connection of signal and slots is left to runtime and implemented as simple string matching (SIGNAL and SLOT macros expand to `const char*`). – mip Dec 29 '12 at 01:16