Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emit signals in another class in Qt?

Tags:

c++

signals

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;
};
like image 442
user1899020 Avatar asked Sep 20 '13 13:09

user1899020


2 Answers

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.

like image 141
Dmitry Sazonov Avatar answered Sep 28 '22 14:09

Dmitry Sazonov


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)

like image 30
ratchet freak Avatar answered Sep 28 '22 13:09

ratchet freak