How to emit signals in another class in Qt?

We can emit signals in a class that easily defines signals on emit signal_a() , for example

 class A { signals: signal_a(); public: void fun() { do_something(); emit signal_a(); do_something(); } }; 

However, how to emit signals in another class in Qt? for example

 class B { public: void fun() { do_something(); (*a) emit signal_a(); // ??? do_something(); } A* a; }; 
+7
c ++ qt signals
source share
3 answers

In Qt5 you can just do

 emit a->signal_a(); 

emit is an empty macro and signals become public (the signals "keyword is a macro that becomes public )

+8
source share

You cannot radiate signals directly because the signals are protected methods (in Qt4). There are several ways to do what you want:

  • create an open method in class A that will generate the necessary signals
  • create a signal in class B and connect it to a signal in class A

You should remember that classes with signals must interhit QObject and contain the Q_OBJECT macro.

+11
source share

Qt signals are conventional methods. The 'emit' keyword will expand to an empty string, so just call a.signal_a ();

+4
source share

All Articles