Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Signals and Slots object disconnect?

I am wondering if i need to disconnect singals and slots if i destroy the signal emitting object. Here is an example:

QAudioOutput * audioOutput = new QAudioOutput(format,mainWindow); connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State)));  delete audioOutput;  audioOutput = new QAudioOutput(format,mainWindow); connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State))); 

Will this automatically disconnect the signal from the old audioOutput, or will it lead to mem leaks or some other undefined behavior ?

Thank you in advance.

like image 236
Anton Avatar asked Feb 13 '12 17:02

Anton


People also ask

How do you turn off signals in Qt?

QMetaObject::Connection myConnect; myConnect=connect(myReadTimer,SIGNAL(timeout()),this,SLOT(ReadMyCom())); ... disconnect(& myConnect);

How signals and slots work in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.


2 Answers

The signals are automatically disconnected when you call the QObject destructor. Have a look at the Qt documentation: QObject Destructor

like image 154
Adrien BARRAL Avatar answered Oct 06 '22 21:10

Adrien BARRAL


You don't have to manually disconnect() signals and slots, the QObject destruction cleans them up automatically.

like image 36
Frank Osterfeld Avatar answered Oct 06 '22 20:10

Frank Osterfeld