Results 1 to 6 of 6

Thread: Qt Query

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Qt Query

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Query

    Quote Originally Posted by anh5kor View Post
    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.

    Quote Originally Posted by anh5kor View Post
    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.

    Quote Originally Posted by anh5kor View Post
    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    anh5kor (23rd December 2015)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Query

    Quote Originally Posted by anh5kor View Post
    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.
    Quote Originally Posted by anh5kor View Post
    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.
    Quote Originally Posted by anh5kor View Post
    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.

  5. The following user says thank you to yeye_olive for this useful post:

    anh5kor (23rd December 2015)

  6. #4
    Join Date
    Dec 2015
    Posts
    22
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt Query

    Then how does the emitter know if the signal was received or not?

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt Query

    Quote Originally Posted by skypuppy View Post
    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,
    _

  8. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Query

    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.

Similar Threads

  1. Replies: 7
    Last Post: 16th April 2015, 17:11
  2. General Query
    By SC in forum General Discussion
    Replies: 0
    Last Post: 2nd November 2013, 10:03
  3. How to get value from a query statement ?
    By hohoanganh205 in forum Newbie
    Replies: 3
    Last Post: 5th January 2012, 14:23
  4. SQL query
    By JD2000 in forum Newbie
    Replies: 4
    Last Post: 1st December 2009, 14:21
  5. MS SQL Query
    By baray98 in forum General Programming
    Replies: 0
    Last Post: 14th July 2009, 04:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.