Results 1 to 4 of 4

Thread: Custom signal

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Custom signal

    I've got a context menu whose triggered() signal I've connected to another widget's slot, and this works fine.
    But I want to send some more information along with this, so I decided I need a custom signal. I read the docs, and I figure I need to do something like the following:

    - Connect the triggered() signal of the menu item to a local slot
    - Connect the custom signal of the class to wherever I need it to go
    - In the local slot, emit my custom signal

    So my code would look like this:

    Qt Code:
    1. // Class declaration
    2. class Foo : public QObject
    3. {
    4. Q_OBJECT
    5.  
    6. ....
    7.  
    8. signals:
    9. void MySignal(int someData);
    10.  
    11. private slots:
    12. void MyLocalSlot();
    13. }
    14.  
    15. ...
    16.  
    17. // Somewhere in the local class
    18. connect(action, SIGNAL(triggered()), this, SLOT(MyLocalSlot()));
    19. connect(this, SIGNAL(MySignal(int), remoteObject, SLOT(OnMySignal(int)));
    20.  
    21. // Local slot
    22. void Foo::MyLocalSlot()
    23. {
    24. emit MySignal(5);
    25. }
    26.  
    27.  
    28. // Destination class
    29. void DestinationClass::OnMySignal(int someData)
    30. {
    31. ...
    32. }
    To copy to clipboard, switch view to plain text mode 

    This works just fine, but I'm wondering if I'm overcomplicating things, or this is the simplest way to do it?

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Custom signal

    Quote Originally Posted by JovianGhost View Post
    This works just fine, but I'm wondering if I'm overcomplicating things, or this is the simplest way to do it?
    If its working fine, and you know what you have done, then who cares

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Custom signal

    You could have used
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    Although your code also seems ok.

  4. #4
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom signal

    Good to know, thanks!

Similar Threads

  1. creating custom signal
    By aj2903 in forum Qt Programming
    Replies: 10
    Last Post: 18th November 2021, 10:33
  2. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 10:28
  3. how to add custom signal to designer
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2009, 11:26
  4. Custom signal in qt designer
    By txandi in forum Qt Tools
    Replies: 1
    Last Post: 4th December 2008, 21:25
  5. custom slot/signal
    By awnjoor in forum Newbie
    Replies: 17
    Last Post: 17th July 2006, 09:05

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.