How to connect signal to this member function using the new method?
Solved
General and Desktop
-
I based my code on this example:
void someFunction(); QPushButton *button = new QPushButton; QObject::connect(button, &QPushButton::clicked, someFunction);
In a header file I have:
class MainWindow : public QMainWindow { Q_OBJECT ... private: void select_filter(int); QComboBox *unit_filters; };
Then in a source file I have:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { ... unit_filters = new QComboBox; QObject::connect(unit_filters, &QComboBox::currentIndexChanged, select_filter); // or this QObject::connect(unit_filters, &QComboBox::currentIndexChanged, [this]{select_filter(0);}); } void MainWindow::select_filter(int) { ... }
How can I make it compile? As you can see, I did what the documentation told me.
I am getting 'no matching function' and 'unresolved overload' etc.
I am using Qt Creator 3.6.0 based on Qt 5.5.1. -
Hi, you get an error because there are 2 different currentIndexChanged() signals, so you have to tell the compiler which one you want read more here
-
Well the example you show is using a free function, not a member function. Perhaps that's why the first connect() doesn't work. In the second, you are missing the empty argument list "()" in the lambda. I believe this should work:
QObject::connect(unit_filters, &QComboBox::currentIndexChanged, this, &MainWindow::select_filter); // or this QObject::connect(unit_filters, &QComboBox::currentIndexChanged, [this]() { select_filter(0); });