PDA

View Full Version : Qt Query



anh5kor
8th December 2015, 09:12
Hello
I am quite new to Qt I have some Question which am not got satisfactory answer till now.

1.Why Q_OBJECT macro is in the private section of code? What will happen if I provide it in Public section of code?
2.What is return type of connect() function and can we return anything from signal and slot,if yes then how?

anda_skoa
8th December 2015, 10:58
1.Why Q_OBJECT macro is in the private section of code? What will happen if I provide it in Public section of code?

It is just customary to put it right after the opening brace of the class declaration but it doesn't really matter.
It expands to code that has access modifiers as needed.



2.What is return type of connect() function

You could have answered that yourself by looking at the documentation: an object of QMetaObject::Connection, basically a handle to the connection.



and can we return anything from signal and slot,if yes then how?

Since a signal can be connected to multiple slots and a slot can be connected to multiple signals, the return value of a slot is not available at the place of signal emit.

For the same reason it is usually also a bad idea to use an argument type that is writable, e.g. a reference or pointer, but if you really, really, need that, then that's the way to get values from a slot to the place of emit.

Cheers,
_

yeye_olive
8th December 2015, 10:59
1.Why Q_OBJECT macro is in the private section of code?
Presumably because it expands to definitions that may need a visibility specification (like "public:") and there is no way in C++ to restore the current visibility as it was before the macro. By specifying that the current visibility must be private, the macro can restore it by a simple "private:" at the end.

What will happen if I provide it in Public section of code?
Who knows? This is not documented. In the above scenario you will make private all declarations following the macro until the next visibility specification.

2.What is return type of connect() function and can we return anything from signal and slot,if yes then how?
QMetaObject::Connection. As explained in the documentation, it is a handle to the connection itself. Signals and slots do not return anything, because they may be called asynchronously.

skypuppy
28th December 2015, 04:24
Then how does the emitter know if the signal was received or not?

anda_skoa
28th December 2015, 09:17
Then how does the emitter know if the signal was received or not?

The emitter usually doesn't care, but you can check if a signal is connected at all.

Cheers,
_

d_stranz
29th December 2015, 15:58
Then how does the emitter know if the signal was received or not?

The emitter doesn't care, as anda_skoa says. A signal is just an ordinary method in the class that declares it. Qt's MOC compiler automatically generates the code for the method, and it is hidden in the moc_xxx.cpp file that is created during the build. You declare and implement slots yourself. A connect() basically adds a pointer to your slot function to the table of connections maintained by the signal method. When you (or a Qt class) "emits" a signal, that is nothing more than invoking the signal function which in turn calls all of the slot functions for the receivers it is connected to.

It only seems like magic.