1

In Qt if the signal is not overloaded it can be passed to the connect method like this.

QObject::connect(comboBox, &QComboBox::currentTextChanged, [&]()-> void {});

But if the signal is overloaded then it can be done in two steps.

Example:

In Qt's QComboBox class the highlighted method is overloaded

void QComboBox::highlighted(int index)
void QComboBox::highlighted(const QString & text)

When using the QObject::connect we can declare the pointer to member function variable and then use it which requires 2 steps.

       void (QComboBox::*fptr) (int) = &QComboBox::highlighted;
            QObject::connect(comboBox, fptr, [&]()-> void {
                                 insertWidgetToMapAndSend(listView);
                             });

Is it possible to pass the overloaded method without the declaration of ftptr ?

Talespin_Kit
  • 20,830
  • 29
  • 89
  • 135

1 Answers1

6

You can cast inline:

QObject::connect(comboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });

But starting with Qt 5.7, use qOverload:

QObject::connect(comboBox, qOverload<int>(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });

or QOverload in pre C++14:

QObject::connect(comboBox, QOverload<int>::of(&QComboBox::highlighted), [&]()-> void {
                             insertWidgetToMapAndSend(listView);
                         });
king_nak
  • 11,313
  • 33
  • 58