-2

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?

Denis Turgenev
  • 138
  • 1
  • 9
  • 1
    The second is the old signal/slot style syntax, the first the new one. – chehrlic Jun 13 '21 at 14:21
  • Hi, @chehrlic I have one question. QCombobox has two signal void currentIndexChanged(int index); void currentIndexChanged(const QString &); and when I use new-style connect, it says ambigous compile error. I couldn't see this error in old-style. Any idea? – Denis Turgenev Jun 22 '21 at 11:54
  • Then you have to use QOverload as explained in the documentation to [QComboBox::currentIndexChanged(int)](https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged) – chehrlic Jun 22 '21 at 12:01

1 Answers1

1

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.

asitdhal
  • 621
  • 2
  • 7
  • 15
  • 1
    clear explanation for me. really appreciated for it. – Denis Turgenev Jun 14 '21 at 14:10
  • these days, do developers yet use SINGAL(), SLOT()? or deprecated? I upvoted to you :) Because I have big project developing since 3 months ago, and I want to know I have to replace all connects to newer version or not. – Denis Turgenev Jun 20 '21 at 16:12
  • Some old developers use SIGNAL and SLOT, but I prefer the member function pointer. It's faster and more obvious. The compiler can detect errors in the member function pointer versions, which is not possible in SIGNAL and SLOT. In Qt ecosystem, SIGNAL and SLOT mechanism is called old way of connecting. So I avoid it. – asitdhal Jun 20 '21 at 18:00
  • So I was an old developer. I am old now. phew ~ – Denis Turgenev Jun 20 '21 at 18:24
  • ha ha...I was kidding. But it's usually done by people who did development 5 - 10 years back, but now they don't need to upgrade the skillset. When a provide a bug fix, they still use SIGNAL and SLOT. – asitdhal Jun 20 '21 at 19:52
  • Hi, @sitdhal I have one question. QCombobox has two signal void currentIndexChanged(int index); void currentIndexChanged(const QString &); and when I use new-style connect, it says ambigous compile error. I couldn't see this error in old-style. Any idea? – Denis Turgenev Jun 22 '21 at 11:53
  • connect(ui->comboBox_parentPolygon, &QComboBox::currentIndexChanged, this, &ChildPolygonAddDialog::slotParentChanged); – Denis Turgenev Jun 22 '21 at 11:56
  • @DenisTurgenev currentIndexChanged is overloaded, so to use function pointer, you need to use the convenient function `QOverload::of` or `static_cast`. Here is the documentation for this https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged. There is also a good answer here https://stackoverflow.com/questions/31164574/qt5-signal-slot-syntax-w-overloaded-signal-lambda – asitdhal Jun 22 '21 at 20:01