Results 1 to 5 of 5

Thread: Emit one signal from multiple classes

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Emit one signal from multiple classes

    According to the documentation on signals and slots: "Only the class that defines a signal and its subclasses can emit the signal." Is there an elegant way to circumvent this restriction? I would like to be able to emit a signal from multiple classes. Thanks for your help!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Emit one signal from multiple classes

    Well you can define the same signal in all your classes or create a minimal class defining the signal and use it as a "global" base class. (I am not sure why someone want to do so, so can you tell us what you are trying.)

    Or if I don't get you right and you want emit a signal of a foreign class, then you have to create a public function which emits the signal, so that you can call that function from your other classes. (?)

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Emit one signal from multiple classes

    Those are good suggestions, thanks! Just to clarify, the situation I have is this: I have a simple GUI (QMainWindow) as well as a class that reads from a serial port (I'm using qextserialport, which has its own thread). When certain bytes are read from the serial port, I would like to emit a signal that the main GUI responds to. The same signal can be emitted from within the GUI too (which is where the signal is defined), hence the desire to have a 'global' signalling mechanism. For now, I've been able to work around the issue by simply avoiding the signal/slot mechanism in my serial port class and calling the required functions in my GUI class directly instead of emitting a signal.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Emit one signal from multiple classes

    Your serial thread is calling functions in your gui class directly?

    But it would seem that you would be better off using an event. They can be generated and send from anywhere.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Emit one signal from multiple classes

    Quote Originally Posted by Ishmael View Post
    For now, I've been able to work around the issue by simply avoiding the signal/slot mechanism in my serial port class and calling the required functions in my GUI class directly instead of emitting a signal.
    I don't see what needs a work-around here. Your GUI class defines a signal that you connect to a slot that is also defined in the GUI class (although you could call it directly from here). The serial port management class also defines a signal that you connect to the GUI object's slot. Whether you name the signals identically is irrelevant.

Similar Threads

  1. Replies: 3
    Last Post: 6th October 2010, 08:33
  2. emit signal from a slot?
    By ask8y@yahoo.com in forum Qt Programming
    Replies: 9
    Last Post: 11th June 2010, 20:18
  3. Emit signal from thread
    By sisco in forum Newbie
    Replies: 2
    Last Post: 26th November 2009, 13:32
  4. how to know which button emit the signal?
    By coder1985 in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2008, 14:26
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.