简体   繁体   中英

How to emit signals in another class in Qt?

We can emit signals in the class defining the signals easily by emit signal_a() like

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;
};

You can't emit signals directly, because signals are protected methods (in Qt4). There are several ways to do what you want:

  1. create public method in class A, that will emit necessary signals
  2. create 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 Q_OBJECT macro.

In Qt5, you can just do

emit a->signal_a();

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM