Qt Signals and Slots object disconnect?

北城以北 提交于 2019-11-26 20:49:45

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/9264750/qt-signals-and-slots-object-disconnect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!