no matching function for QObject::connect
-
Hello,
I'm new to Qt and I'm trying to do a little app as a personal project.
I created a Widget with a series of QPushButton and now I'm trying to connect the button to their Slot. Here is a code that should reproduce the same error I get:starting_widget.h
#ifndef STARTING_WIDGET_H #define STARTING_WIDGET_H #include <QWidget> #include <QPushButton> namespace Ui { class Starting_widget; } constexpr int EXIT_ID = 00; class Starting_widget : public QWidget { Q_OBJECT public: explicit Starting_widget(QWidget *parent = nullptr); ~Starting_widget(); signals: void exit_clicked(); private slots: void button_pushed(int button_id); private: Ui::Starting_widget *ui; QPushButton *exit_button; }; #endif // STARTING_WIDGET_H
starting_widget.cpp
#include "starting_widget.h" #include "ui_starting_widget.h" Starting_widget::Starting_widget(QWidget *parent) : QWidget(parent), ui(new Ui::Starting_widget), exit_button(new QPushButton("EXIT")), { ui->setupUi(this); ui->gridLayout->addWidget(new_game_button,0,0); connect(exit_button,SIGNAL(QPushButton::clicked()),this,SLOT(button_pushed(EXIT_ID))); } Starting_widget::~Starting_widget() { delete ui; delete exit_button; } void Starting_widget::button_pushed(int button_id){ if(button_id==EXIT_ID) emit exit_clicked(); }
main.cpp
#include "starting_widget.h" #include <QApplication> #include <QPushButton> int main(int argc, char *argv[]) { QApplication app(argc, argv); Starting_widget *starting_page= new Starting_widget; QObject::connect(starting_page, SIGNAL(exit_clicked()), app, SLOT(quit())); starting_page->show(); return app.exec(); }
I get the following error:
main.cpp:7: error: no matching function for call to ‘QObject::connect(Starting_widget*&, const char*, QApplication&, const char*)’ main.cpp: In function ‘int main(int, char**)’: main.cpp:7:21: error: no matching function for call to ‘QObject::connect(Starting_widget*&, const char*, QApplication&, const char*)’ 7| QObject::connect(starting_page,SIGNAL(exit_clicked()),app, SLOT(quit()));
I looked online and the only cases of similar errors I found were caused by a missing Q_OBJECT in the .h file, but in my case, I have this macro at the right place (I think) so I cannot understand what is the problem. Any help is accepted!
-
@antoninodanna
I suggest you read New Signal Slot Syntax and change over before you do anything else. This was introduced a decade ago. -
@antoninodanna The target object in a connect call is identified by a pointer, not the object itself.
So this is incorrect:
QObject::connect(starting_page, SIGNAL(exit_clicked()), app, SLOT(quit()));
but could be:
QObject::connect(starting_page, SIGNAL(exit_clicked()), &app, SLOT(quit())); // or this QObject::connect(starting_page, SIGNAL(exit_clicked()), qApp, SLOT(quit()));
or you could use the "new" style connects which give you feed back on problems like this at compile time.
There are other problems with the connect() in Starting_widget::Starting_widget().
-
He did :-P At least half way :)
@antoninodanna said in no matching function for QObject::connect:
connect(exit_button,SIGNAL(QPushButton::clicked()),this,SLOT(button_pushed(EXIT_ID)));
I dont know in what tutorial you found
connect(exit_button,SIGNAL(QPushButton::clicked()),this,SLOT(button_pushed(EXIT_ID)));
but it's wrong in multiple ways.
The bold part is some kind of mix between the string based and the function pointer based connection style.
Even when sticking to the former one,SLOT(button_pushed(EXIT_ID))
is also wrong because you need a type there and no variable names (soint
instead of yourEXIT_ID
variable whose value you can transmit anyway)