How to connect signal to this member function using the new method?
Solved
General and Desktop
-
wrote on 1 Mar 2016, 13:29 last edited by
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. -
wrote on 1 Mar 2016, 13:37 last edited by
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
-
wrote on 1 Mar 2016, 13:50 last edited by JordanHarris 3 Jan 2016, 13:52
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); });
-
wrote on 1 Mar 2016, 14:00 last edited by
Thank you, hskoglund. I added a cast to the signal.
static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged)
1/4