Disconnect doesn't happen upon deletion
-
You are deleting slotObject before the event loop starts thus you won't get anything related to signals and slots.
-
[quote author="SGaist" date="1369120421"]You are deleting slotObject before the event loop starts thus you won't get anything related to signals and slots.
[/quote]what about code in first post, slotObject.deleteLater (); in that case object will be deleted by event loop and in that case i should also expect the signal, right.?
Or may be *void disconnectNotify (const char signal); won't work for the object which is getting deleted, because if the object is deleted then all pending events are flushed, I think.
-
It might also be deleted one the first loop, then was it completely "set up" ?
Did you try to use QTimer::singleShot with a timeout of a second ? -
[quote author="SGaist" date="1369125092"]It might also be deleted one the first loop, then was it completely "set up" ?
Did you try to use QTimer::singleShot with a timeout of a second ?[/quote]sorry, didn't get you...
-
Meaning: did you try something like
@
int main (int argc, char **argv)
{
QCoreApplication core (argc, argv);
SlotClass *slotObject;slotObject = new SlotClass (); slotObject->MakeConnection (); QTimer::singleShot(1000, slotObject, SLOT(deleteLater())); return core.exec ();
}@
?
-
@SGaist, Yes, I tried the code that you mentioned, it gave me same result... :-)
Timer deletes the slotObject after delay of 1 second but didn't see any disconnection prints. -
When your slotObject is deleted, your signalObject is also deleted immediately (because it is a member variable!) -- that's why it has no time to call disconnectNotify().
Separate the two objects and make sure that signalObject doesn't get destroyed with slotObject. You should see the debug message then:
@
int main(...)
{
...
SignalClass *signalObject = new SignalClass();
SlotClass *slotObject = new SlotClass();QObject::connect(signalObject, SIGNAL(...), slotObject, SLOT(...)); slotObject->deleteLater(); ...
}
@ -
Argle, I misread the code... Didn't saw that signalObject was a member object...
-
Ok, I removed signalObject from slotClass
and modified main.cpp as follows,@int main (int argc, char **argv)
{
QCoreApplication core (argc, argv);
SignalClass *signalObject;
SlotClass *slotObject;signalObject = new SignalClass (); slotObject = new SlotClass (); QObject::connect (signalObject, SIGNAL(sigNotification()), slotObject, SLOT(SlotNotification())); slotObject->deleteLater (); return core.exec ();
}@
It gave me,
Console Output
SignalClass Constructor
SlotClass Constructor
Signal Connected
SlotClass Destructor -
It looks like your program quit immediately. That would also not give signalObject time to call disconnectNotify() ;)
Turn it into a GUI application and show a QWidget -- that will keep the program running until you close the window:
@
int main (int argc, char **argv)
{
QApplication a(argc, argv);
QWidget w;
w.show();SignalClass ...
}
@You'll also need to add "QT += widgets" (or "QT += gui" if you're using Qt 4) to your .pro file.
I just tried this using Qt 5.0.2 in Windows, and it worked.
-
In that case I would say that I am running core.exec(), so surely program doesn't get terminated or quits immediately. Event loop keeps on running until I click the stop button in Qt creator.
-
Hmm... it might be a bug in Qt 4 or your platform (what OS are you using?). I made this is using Qt 5.0.2 (MSVC2012 x64) on Windows 8 x64:
@
class MyWidget : public QWidget
{
Q_OBJECT
QString name;public:
MyWidget(const QString& name, QWidget *parent = 0) : QWidget(parent), name(name)
{ qDebug() << name << "constructed"; }~MyWidget() { qDebug() << name << "destroyed"; }
protected:
void connectNotify(const QMetaMethod &signal)
{ qDebug() << name << "connected to" << signal.name(); }void disconnectNotify(const QMetaMethod &signal) { qDebug() << name << "disconnected from" << signal.name(); }
signals:
void mySignal();public slots:
void mySlot() {}
};
@@
int main(int argc, char *argv[])
{
QApplication a(argc, argv);MyWidget *w1 = new MyWidget("w1"); MyWidget *w2 = new MyWidget("w2"); QObject::connect(w1, SIGNAL(mySignal()), w2, SLOT(mySlot())); w1->show(); w2->deleteLater(); return a.exec();
}
@Console output (Debug build)
[quote]"w1" constructed
"w2" constructed
"w1" connected to "mySignal"
"w1" connected to "objectNameChanged"
"w2" destroyed
"w1" disconnected from "mySignal"[/quote]Interestingly, I got this final line when I close the window in Qt 5.1.0 beta 1, but not Qt 5.0.2:
[quote]"w1" disconnected from "objectNameChanged"[/quote]