stack object Qt signal and parameter as reference

后端 未结 4 1844
北恋
北恋 2020-12-13 04:35

May I have a \"dangling reference\" with the following code (in an eventual slot connected to the myQtSignal)?

class Test : public QObject
{
    Q_OBJECT

si         


        
4条回答
  •  [愿得一人]
    2020-12-13 05:04

    I'm sorry to continue a subject years old but it came up on Google. I want to clarify HostileFork's answer as it may mislead future readers.

    Passing a reference to a Qt signal is not dangerous thanks to the way signal/slot connections work:

    • If the connection is direct, connected slots are directly called directly, e.g. when emit MySignal(my_string) returns all directly connected slots have been executed.
    • If the the connection is queued, Qt creates a copy of the referencees. So when the slot is called it has its own valid copy of the variables passed by reference. However this means that parameters must be of a type that Qt knows about in order to copy it.

    http://qt-project.org/doc/qt-5.1/qtcore/qt.html#ConnectionType-enum

提交回复
热议问题