Qt5信号槽的新语法与lambda函数

New Connection Syntax

In addition to the still available well known way to connect signals and slots, new overloads of QObject::connect have been added allowing you to connect signals and slot using pointer to member functions:

QObject::connect(sender, &Sender::valueChanged, receiver, &Receiver::updateValue);

The new syntax provides compile time checking of the compatibility between signals and slots, automatic conversion of the arguments if there is an implicit cast between the types, and solve many problems occurring because of typedef or namespaces.

Additionally, if your compiler supports C++11 lambdas, you can even connect directly to a lambda function.

connect(sender, &QObject::destroyed, [=](){ this->m_objects.remove(sender); });


参考:

信号与槽的新语法(Qt5)

New Signal Slot Syntax in Qt 5

Using C++11 Lambdas As Qt Slots

Differences between String-Based and Functor-Based Connections

posted @ 2017-08-23 16:26  XXX已失联  阅读(757)  评论(0编辑  收藏  举报