I can't understand the new syntax of connect
-
The definition of connect is as below:
inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, const char *amember, Qt::ConnectionType atype) const { return connect(asender, asignal, this, amember, atype); }
And I can't understand how it work like
connect(new QSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), [=](auto v){});
Why function pointer and lambda function can convert to
const char *
?
-
Why function pointer and lambda function can convert to const char *
Directly they can't and they don't in this example. You've got the wrong overload. Here's the list: connect overloads.
The one used here is
template<typename PointerToMemberFunction, typename Functor> QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
-
The definition of connect is as below:
inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, const char *amember, Qt::ConnectionType atype) const { return connect(asender, asignal, this, amember, atype); }
And I can't understand how it work like
connect(new QSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), [=](auto v){});
Why function pointer and lambda function can convert to
const char *
?
This post is deleted! -
The definition of connect is as below:
inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal, const char *amember, Qt::ConnectionType atype) const { return connect(asender, asignal, this, amember, atype); }
And I can't understand how it work like
connect(new QSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), [=](auto v){});
Why function pointer and lambda function can convert to
const char *
?
@Mr-Pang said in I can't understand the new syntax of connect:
static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged)
P.S. It is nicer to write
QOverload<int>::of(&QSpinBox::valueChanged)
(C++11), ORqOverload<int>(&QSpinBox::valueChanged)
(C++14 and newer)
P.P.S. You might find this article helpful: https://doc.qt.io/qt-5/signalsandslots-syntaxes.html
-
Why function pointer and lambda function can convert to const char *
Directly they can't and they don't in this example. You've got the wrong overload. Here's the list: connect overloads.
The one used here is
template<typename PointerToMemberFunction, typename Functor> QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
@Chris-Kawa
OK. I just use qtcreator to jump and it takes me to wrong place. -
@Mr-Pang said in I can't understand the new syntax of connect:
static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged)
P.S. It is nicer to write
QOverload<int>::of(&QSpinBox::valueChanged)
(C++11), ORqOverload<int>(&QSpinBox::valueChanged)
(C++14 and newer)
P.P.S. You might find this article helpful: https://doc.qt.io/qt-5/signalsandslots-syntaxes.html
-
@Mr-Pang said in I can't understand the new syntax of connect:
I think doc.qt.io may update the doc about QSpinBox since my syntax is copied from there.
The latest version of the documentation (Qt 5.13.1) uses QOverload: https://doc.qt.io/qt-5/qspinbox.html
Anyway, has your original question been answered? Do you understand the new connect syntax now?