Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. no matching function for QObject::connect
Forum Updated to NodeBB v4.3 + New Features

no matching function for QObject::connect

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 333 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antoninodanna
    wrote on last edited by
    #1

    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!

    JonBJ C 2 Replies Last reply
    0
    • A antoninodanna

      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!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @antoninodanna
      I suggest you read New Signal Slot Syntax and change over before you do anything else. This was introduced a decade ago.

      Pl45m4P 1 Reply Last reply
      2
      • A antoninodanna

        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!

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @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().

        1 Reply Last reply
        2
        • JonBJ JonB

          @antoninodanna
          I suggest you read New Signal Slot Syntax and change over before you do anything else. This was introduced a decade ago.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @JonB

          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 (so int instead of your EXIT_ID variable whose value you can transmit anyway)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved