I am currently encountered two connecting ways in qt.
connect(openAction, &QAction::triggered, this, &MyMainWindow::openFile);
connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
Can anyone point difference and when to use which?
I am currently encountered two connecting ways in qt.
connect(openAction, &QAction::triggered, this, &MyMainWindow::openFile);
connect(openAction, SIGNAL(triggered()), this, SLOT(openFile()));
Can anyone point difference and when to use which?
SIGNAL means the string representation of the function and &QAction::triggered is a function pointer. SIGNAL is the old way of doing things and slow due to string comparison at run time.
The following line of code will resolve to a connect which uses string comparison of signal and slot.
connect(this, SIGNAL(done(int)), this, SLOT(onDone(int)));
The candidate of connect being invoked
inline QMetaObject::Connection QObject::connect(
const QObject *asender, const char *asignal,
const char *amember, Qt::ConnectionType atype) const
The function pointer version will invoke a connect which uses type information and function address.
connect(this, &MainWindow::done, this, &MainWindow::onDone);
The candidate function of connect is one of the many overload sets which takes a member function pointer.
//Connect a signal to a pointer to qobject member function
template <typename Func1, typename Func2>
static inline QMetaObject::Connection connect(
const typename QtPrivate::FunctionPointer<Func1>::Object *sender,
Func1 signal,
const typename QtPrivate::FunctionPointer<Func2>::Object *receiver,
Func2 slot,
Qt::ConnectionType type = Qt::AutoConnection)
So the function pointer version of connecting signals and slots is faster.