Results 1 to 4 of 4

Thread: Is it possible to define / implement signal in Qt ?

  1. #1
    Join Date
    Jan 2018
    Location
    India, Delhi
    Posts
    10
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Is it possible to define / implement signal in Qt ?

    What i really want to know is, is it possible to actually "define" signal and have an implementation for it in *.cpp file rather then just having a declaration in .h file, to me it seems like as if it is a completely impossible to have implementation for signal functions
    like for example
    Qt Code:
    1. connect( someObject, SIGNAL( mySignal() ), otherObject , SLOT (otherSlot()));
    To copy to clipboard, switch view to plain text mode 

    now, if implementation of mySignal is as follow :
    Qt Code:
    1. mySignal()
    2. {
    3. qDebug()<<" hello ";
    4. }
    To copy to clipboard, switch view to plain text mode 

    i want to ask, if i emit mySignal(), will it's own body will be executed before the slot function is executed or im getting it completely wrong and signal serve no purpose other than just as an signature that can carry around values and have no implementation of its own.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Is it possible to define / implement signal in Qt ?

    Nice thought

    ...is it possible to actually "define" signal..
    Yes, you can define, but then you will have 2 implantations of the signal, one provided by Qt (qmake) and one by you, so eventually linker will throw error and your program will not build.

    ..if i emit mySignal(), will it's own body will be executed before the slot function is executed...
    When you emit a signal (which is nothing but calling the signal function), signal's body is executed, and in the signal's body there is code to call all the connected slots.
    Qt Code:
    1. // following statements are same
    2. emit mySignal;
    3. mySignal();
    To copy to clipboard, switch view to plain text mode 


    ...im getting it completely wrong and signal serve no purpose other than just as an signature that can carry around values and have no implementation of its own.
    keywork "signal" is used inform the qmake that the following member functions are signals and that the functions must be implemented by the qmake.

    Basically signal or slot is just a plain cpp member functions. In the context of Qt, qmake provides the implementation of the signals, you don't (and should not) implement the signals. In the Qt provided implementation of the signal, all the connected slots are called.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    keshav2010 (26th January 2018)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,243
    Thanks
    303
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Is it possible to define / implement signal in Qt ?

    What i really want to know is, is it possible to actually "define" signal and have an implementation for it in *.cpp file rather then just having a declaration in .h file, to me it seems like as if it is a completely impossible to have implementation for signal functions
    To add to what Santosh said, a method declared as a "signal" in a class definition is implemented when your code is pre-compiled by MOC (the Qt Meta-Object Compiler). You can see this code in the "moc_xyz.cpp" (where "xyz" is the name of your class) file that is created in the GeneratedFiles subdirectory of your project. The implementation of your signal created by MOC goes through each of the slots that were connected to your signal, in the order in which they were connected, and calls the slot with the arguments you provided when you emitted your signal.

    It is completely boilerplate code, which is why it can be generated simply from the declaration of your signal in the class header.

    By asking if you can implement your own code for a signal, I think you misunderstand what signals are for. They are solely to notify some listener (slot) that something has changed.

    So if your code needs to do something prior to emitting its signal, then you do it in some method that at the end emits the signal.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    keshav2010 (26th January 2018)

  6. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to define / implement signal in Qt ?

    +1 to what d_stranz said - very good answer.
    Continuing with d_stranz's line, you actually can do something to the effect of what you are talking about by having a regular signal connected to a lambda function slot.
    Some more information: https://wiki.qt.io/New_Signal_Slot_Syntax
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following 2 users say thank you to high_flyer for this useful post:

    d_stranz (26th January 2018), keshav2010 (26th January 2018)

Similar Threads

  1. Help! How to implement CHECK & RETURN in signal/slots
    By apollocheun in forum Qt Programming
    Replies: 4
    Last Post: 23rd July 2010, 10:51
  2. How to Implement signal and slots for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 07:57
  3. tr with #define..it can work?
    By mattia in forum Newbie
    Replies: 9
    Last Post: 4th February 2008, 12:15
  4. #define MYCLASS_H ?
    By bnilsson in forum General Programming
    Replies: 1
    Last Post: 3rd February 2008, 11:50
  5. Replies: 1
    Last Post: 4th November 2006, 07:53

Tags for this Thread

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.