Solved Qt emit signal after widget is closed
-
I hope this is good
Thanks!
-
@pmanc Looks like the signal is still emitted. Can you try to disconnect manually before calling deleteLater() to see whether it still crashes?
-
@jsulm I added
disconnect(ad, nullptr, nullptr, nullptr);
before deleteLater, but still crashes, plus at live 295 I have a semantic error:
moc_advanceddialog.cpp:295:5: error: no matching function for call to 'activate' qobjectdefs.h:397:17: note: candidate function not viable: no known conversion from 'AdvancedDialog *' to 'QObject *' for 1st argument qobjectdefs.h:398:17: note: candidate function not viable: no known conversion from 'AdvancedDialog *' to 'QObject *' for 1st argument qobjectdefs.h:396:17: note: candidate function not viable: requires 3 arguments, but 4 were provided
-
@pmanc QObject::Connect returns a
QMetaObject::Connection
you're supposed to store that and call disconnect on that object -
@J-Hilk
Sorry, i didn't know.
Now I haveAdvancedDialog *ad = new AdvancedDialog(this); QMetaObject::Connection c = connect(ad, SIGNAL(errorTcpSignal(QString)), this, SLOT(displayTcpError(QString))); ad->exec(); disconnect(c); ad->deleteLater();
But still crashes, this SIGNAL is emitted.
-
disconnect
won't stop the signal emitting, just stop calling the slot.
I think you should disconnect the other connection, from that SocketError.
That connection doesn't have a receiver context, so the lambda will be called even afterthis
is destroyed.
How about addthis
as the receiver context:connect(&t, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, [=](QAbstractSocket::SocketError err){ emit errorTcpSignal(...); });
-
@Bonnie
Thank you very much for your reply.
The problem is that I want the signal to be emitted when I close the QDialog too.
I have MainWindow with the slot for the error, and the QDialog that emits the signal.
The core of the problem is that I want to receive the signal from QDialog 's QtConcurrent thread also after QDialog has been closed. -
From your screen capture, the
errorTcpSignal
is a member fromAdvancedDialog
, right?
Where do you emit it then? I'm not quite understanding your code since I don't know what isthis
in those connections.
If you want the dialog to emit the signal, then you can't delete it. -
@Bonnie
Yes, errorTcpSignal is a member from AdvancedDialog.
I emit it from a QtConcurrent thread in AdvancedDialog when an AdvancedDialog 's button is pressed.
If I don't delete it everything works, but I'll have a memory leak. -
@pmanc said in Qt emit signal after widget is closed:
If I don't delete it everything works, but I'll have a memory leak.
No, you won't.
It have a parent widget (in your code when younew
it), so it will be deleted after the parent is destoyed.
But I still don't think it is a right design since you want to delete the dialog but also need it to send signals.
Though I don't fully understand your logic, I think you should handle the QtConcurrent related code in your QMainWindow. -
@Bonnie
I see. But every time I open a new AdvancedDialog I see my memory use to grow in the task manager.
Should I initialize AdvancedDialog once in the MainWindow constructor and show / hide it when I need?By the way, great community, everyone!
-
@pmanc said in Qt emit signal after widget is closed:
But every time I open a new AdvancedDialog
If you need this dialog often then don't delete it and don't recreate it each time you need it. Create it once.
-
@pmanc said in Qt emit signal after widget is closed:
Should I initialize AdvancedDialog once in the MainWindow constructor and show / hide it when I need?
If you want to keep handling QtConcurrent related things in the dialog , I think you should.
-
Ok thank you very much for your time!