no matching member function for call to 'connect' - simple example why doesn't this work??
-
Hi,
I made a very simple test project, just a QMainWindow. I instantiated a QSpinBox in the constructor and tried to connect it to a public slot of MainWindow. Yet I get the error "no matching member function for call to 'connect'. This using both the old and new style connect syntax. My header includes Q_OBJECT. Both QMainWindow and QSpinBox derive from QObject.
I must be missing/forgetting something very simple here. Why on earth doesn't this work?
mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void doTest(int val); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSpinBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QSpinBox* oSpinBox = new QSpinBox(this); connect(oSpinBox, &QSpinBox::valueChanged, this, &MainWindow::doTest); } MainWindow::~MainWindow() { delete ui; } void MainWindow::doTest(int val) { // do something }
-
@pmh4514 said in no matching member function for call to 'connect' - simple example why doesn't this work??:
valueChanged
value changed has 2 overloads int and string, the new connect does not know how to handle that,
but the spinbox documentation actually offers you the solution:
-
@pmh4514 said in no matching member function for call to 'connect' - simple example why doesn't this work??:
onnect(oSpinBox, &QSpinBox::valueChanged, this, &MainWindow::doTest);
Problem is that valuChanged is overloaded so you must use other connect's construction.
Try this:connect(oSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::doTest);
-
I posted all the code and the error message exactly.
Then as suggested I tried making the QSpinBox a member variable of MainWindow so that it would outlive the constructor.
I still got the "no matching member function for call to 'connect' error.
However, if instead I use the old style connect syntax that explicitly specifies the valueChanged(int) interface, then it works:
connect(oSpinBox, SIGNAL(valueChanged(int)), this, SLOT(doTest(int)));
The documentation linked above and its use of QOverload answers the rest.
Thanks everyone!
-
@pmh4514 said in no matching member function for call to 'connect' - simple example why doesn't this work??:
However, if instead I use the old style connect syntax that explicitly specifies the valueChanged(int) interface, then it works:
Yes: https://doc.qt.io/qt-5/signalsandslots-syntaxes.html#selecting-overloaded-signals-and-slots