When the Qt-5 fails, plug in

Reading The documentation on Qt signals and slots , it seems that the only reason for the failure of the new style is: "If there is already a duplicate (the same signal for the same slot on the same objects), the connection will fail and the connection will return false"

This means that the connection was already successful for the first time and does not allow multiple connections when using Qt :: UniqueConnection.

Does this mean that the Qt-5 style mix will always succeed? Are there any other reasons for refusing?

+4
source share
3 answers

The new style connectmay still work at runtime for various reasons:

  • sender, receiver - . , , .
  • PMF, , . ++, , , , - .

    , : signals: . moc , , , . , , connect, , connect , ( ).

  • -. , TU:

    • one - TU, moc- ( moc_class.cpp). TU , , , ( -).
    • - TU, connect(sender, &Sender::signal, ...), , .

    TU , , , , , , ..; ABI .

    , 1., , 2; , (. , , GNU ld ARM , ).

    Qt / , , , . , Qt 5.9 -Bsymbolic* GCC x86 x86-64.

    , , . , connect return false, .

+4

, , (, nullptr)

QObject* obj1 = new QObject();
QObject* obj2 = new QObject();
// Will succeed
connect(obj1, &QObject::destroyed, obj2, &QObject::deleteLater);
delete obj1;
obj1 = nullptr;
// Will fail even if it compiles
connect(obj1, &QObject::destroyed, obj2, &QObject::deleteLater);
+2

No, this is not always successful. The docs give an example here where it connectreturns false, because the signal should not contain variable names.

// WRONG
QObject::connect(scrollBar, SIGNAL(valueChanged(int value)),
             label, SLOT(setNum(int value)));
-2
source

Source: https://habr.com/ru/post/1675120/


All Articles