3

Say I have form A that can process signal x emitted by form B. When signal x was emitted, form A does something. Then I close form A, reopen form A and wait for signal x. However, the slot for processing signal x is called twice. I repeat closing and reopening form A. the slot for processing signal x is called an increasing number.

It seems old signal still exits even though it was received. Is there any method to clear an old signal?

David Buck
  • 3,752
  • 35
  • 31
  • 35
fucai1116
  • 191
  • 3
  • 5

2 Answers2

4

A signal/slot connection does not disappear when the slot has received a signal. There is no 'emit once' concept. So it seems you are connecting the same signal/slot multiple times. Note that if you would destruct form A, all its connections will get disconnected automatically. So put a breakpoint near the connect statement and test what's going on. Alternatively you could just use an unique connection:

"Qt::UniqueConnection Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6."

Just add Qt::UniqueConnection as the final argument to you're connect call.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Elmar de Koning
  • 519
  • 3
  • 4
  • I tried to use Qt::UniqueConnection, however when I debug, connect was still created again. I solved this by calling disconnect when form A is closed – fucai1116 Jul 25 '11 at 02:17
  • I think when form A is closed by clicking X button on dialog, form A is not really destroyed because I recognize destructor of form A is not called is the reason that the connect still exists. – fucai1116 Jul 25 '11 at 02:24
  • 1
    Pressing the X/ok/cancel/etc button will not destruct the dialog, and with good reason. If it would be destructed then you would not be able to access any user entered data from the dialog once it has been closed. This could be very problematic for modal dialogs. Furthermore, if you created the dialog on the stack how would it be able to destruct itself? – Elmar de Koning Jul 25 '11 at 05:53
1

You should post the part of your code where you open/reopen and close form A.

According to your description it seems that when you reopen your form you are infact creating a new ( different form object) and connecting the same signal over and over again.

You should create form A only once - when you need it the first time - but keep using during your program execution. When reopening it you should only execute formA->show()

Something like

void showFormA() {

    if (myFormA == 0 ) {

        myFormA = new FormA(this);
        //connect signals/slots
        //...
    }

    myFormA->show();
}

EDIT:

About your question and whether you can "clear" an old signal. See http://doc.qt.io/qt-5/qobject.html#disconnect

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
bruno
  • 2,802
  • 1
  • 23
  • 23