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. Error: no matching function for call to 'QObject::connect
QtWS25 Last Chance

Error: no matching function for call to 'QObject::connect

Scheduled Pinned Locked Moved Unsolved General and Desktop
30 Posts 6 Posters 29.0k Views
  • 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.
  • W Offline
    W Offline
    WhatIf
    wrote on 1 Jan 2018, 20:28 last edited by A Former User 1 Mar 2018, 18:00
    #1

    Hi,

    I'm building a simple program to learn how to use widgets, layouts, etc without using the designer but ran through a problem. Basically, the program is designed to have a typical main and MainWindow but instead of having all gui code in MainWindow, I have a class called MainWindowContent which builds the gui and returns the layout containing all the widgets which is added to MainWindow's central widget.

    #include "mainwindowcontent.h"
    
    MainWindowContent::MainWindowContent()
    {
    
    }
    
    QGridLayout* MainWindowContent::getPageWidgetsLayout()
    {
        QGridLayout *gridLayout  =new QGridLayout();
    
        QLabel *label = new QLabel("");
    
        QLineEdit *lineEdit = new QLineEdit("Enter Text!");
    
        QPushButton *button = new QPushButton("OK!");
    
        QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton(label, lineEdit)));
    
        gridLayout->addWidget(label,0,0);
        gridLayout->addWidget(lineEdit,1,1);
        gridLayout->addWidget(button,2,2);
       
    
        return gridLayout;
    }
    
    //slot to handle button clicks
    void MainWindowContent::handleButton(QLabel* label,QLineEdit *lineEdit)
    {
        label->setText(lineEdit->text());
    }
    
    

    I keep getting

    error: no matching function for call to 'QObject::connect(QPushButton*&, const char*, MainWindowContent*, const char*)'
    QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton(label, lineEdit)));

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 1 Jan 2018, 20:42 last edited by
      #2

      @WhatIf said in error: no matching function for call to 'QObject::connect:

      QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton(label, lineEdit)));

      Hi
      That is not correct.
      The clicked() signal do not have a label and lineEdit as parameters so you cannot connect like that.
      the signal must have those parameters too.

      Can i ask what the logic should be ?

      1 Reply Last reply
      4
      • W Offline
        W Offline
        WhatIf
        wrote on 1 Jan 2018, 21:35 last edited by
        #3

        Even if I delete the parameters label and lineEdit from the slot, its function implementation and in the .h file, I still get the same error.

        P 1 Reply Last reply 2 Jan 2018, 05:21
        0
        • W WhatIf
          1 Jan 2018, 21:35

          Even if I delete the parameters label and lineEdit from the slot, its function implementation and in the .h file, I still get the same error.

          P Offline
          P Offline
          Paul Colby
          wrote on 2 Jan 2018, 05:21 last edited by
          #4

          @WhatIf,

          Show us the updated header declaration, updated connect call, and the resulting error message.

          Cheers.

          1 Reply Last reply
          4
          • V Offline
            V Offline
            VRonin
            wrote on 2 Jan 2018, 08:35 last edited by
            #5

            QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));

            https://wiki.qt.io/New_Signal_Slot_Syntax

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            4
            • W Offline
              W Offline
              WhatIf
              wrote on 2 Jan 2018, 14:03 last edited by
              #6
              private slots:
                  void handleButton();
              
              QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
              
              void MainWindowContent::handleButton()
              {
                  //label->setText(lineEdit->text());
              }
              

              error: no matching function for call to 'QObject::connect(QPushButton*&, const char*, MainWindowContent*, const char*)'
              QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));

              I also tried

              QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));
              

              and got

              error: no matching function for call to 'QObject::connect(QPushButton*&, void (QAbstractButton::)(bool), MainWindowContent, std::_Bind_helper<false, void (MainWindowContent::)(QLabel, QLineEdit*), MainWindowContent*, QLabel*&, QLineEdit*&>::type)'
              QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));

              V 1 Reply Last reply 2 Jan 2018, 16:26
              0
              • H Online
                H Online
                hskoglund
                wrote on 2 Jan 2018, 16:20 last edited by
                #7

                Hi, maybe you forgot to inherit from QObject or QWidget, i.e. your mainwindowcontent.h should look something like this:

                #pragma once
                #include "qobject.h"
                
                class MainWindowContent : QObject
                {
                     Q_OBJECT
                
                public:
                    MainWindowContent();
                
                private slots:
                    void handleButton(QLabel* label,QLineEdit *lineEdit);
                }
                
                1 Reply Last reply
                0
                • W WhatIf
                  2 Jan 2018, 14:03
                  private slots:
                      void handleButton();
                  
                  QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                  
                  void MainWindowContent::handleButton()
                  {
                      //label->setText(lineEdit->text());
                  }
                  

                  error: no matching function for call to 'QObject::connect(QPushButton*&, const char*, MainWindowContent*, const char*)'
                  QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));

                  I also tried

                  QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));
                  

                  and got

                  error: no matching function for call to 'QObject::connect(QPushButton*&, void (QAbstractButton::)(bool), MainWindowContent, std::_Bind_helper<false, void (MainWindowContent::)(QLabel, QLineEdit*), MainWindowContent*, QLabel*&, QLineEdit*&>::type)'
                  QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));

                  V Offline
                  V Offline
                  VRonin
                  wrote on 2 Jan 2018, 16:26 last edited by
                  #8

                  @WhatIf said in error: no matching function for call to 'QObject::connect:

                  I also tried
                  QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));

                  and got

                  error: no matching function for call to 'QObject::connect(QPushButton*&, void (QAbstractButton::)(bool), MainWindowContent, std::_Bind_helper<false, void (MainWindowContent::)(QLabel, QLineEdit*), MainWindowContent*, QLabel*&, QLineEdit*&>::type)'
                  QObject::connect(button, &QPushButton::clicked,this, std::bind(&MainWindowContent::handleButton,this,label,lineEdit));

                  read the error. (MainWindowContent::)(QLabel, QLineEdit*) did you forget a * next to QLabel in handleButton?

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    WhatIf
                    wrote on 2 Jan 2018, 19:42 last edited by
                    #9

                    I forgot to subclass QObject!

                    Now the program compiles but the slot is never called. here is how the code looks at this point.

                    signals:
                    
                    private slots:
                        void handleButton(); //handles button clicks
                    
                    QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                    
                    void MainWindowContent::handleButton()
                    {
                        qDebug() << "INSIDE SLOT!";
                    }
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 2 Jan 2018, 19:57 last edited by
                      #10

                      Hi
                      try with
                      qDebug () << "conn:" << QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                      and see it says
                      conn: true

                      1 Reply Last reply
                      0
                      • H Online
                        H Online
                        hskoglund
                        wrote on 2 Jan 2018, 20:12 last edited by
                        #11

                        Hi, also to add to @mrjj, perhaps the slot is never called because the button isn't visible, you could try insert a show() just after you've created the button, say like this:

                        QPushButton* button = new QPushButton("OK!");
                        button->show();
                        
                        QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                        1 Reply Last reply
                        1
                        • W Offline
                          W Offline
                          WhatIf
                          wrote on 2 Jan 2018, 21:37 last edited by
                          #12
                          qDebug () << "conn:" << QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                          

                          shows conn: true

                          using button->show(); doesn't change anything.

                          Basically, main.cpp calls mainwindow.cpp to get the central widget. mainwindow.cpp in turn calls mainwindowcontent.cpp to get the layout to set as centralwidget's.

                          I'm passing "this" (connect 3rd argument) of mainwindowcontent.cpp to the connect statement.

                          Will this make the connect work when mainwindowcontent.cpp returns the layout to mainwindow.cpp?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 2 Jan 2018, 21:46 last edited by
                            #13

                            Hi,

                            Did you check whether you have any warning at run time ?

                            How do you know it's not getting called ? Did you put a breakpoint in your slot or a qDebug statement ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • W Offline
                              W Offline
                              WhatIf
                              wrote on 2 Jan 2018, 22:06 last edited by
                              #14

                              No warnings at run time.

                              I used both breakpoints and a qDebug statement.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 2 Jan 2018, 22:07 last edited by
                                #15

                                Then a silly question: how are you ensuring that you are clicking on the correct button ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                1
                                • W Offline
                                  W Offline
                                  WhatIf
                                  wrote on 2 Jan 2018, 22:28 last edited by
                                  #16

                                  There is only one button

                                  1 Reply Last reply
                                  0
                                  • H Online
                                    H Online
                                    hskoglund
                                    wrote on 2 Jan 2018, 23:40 last edited by
                                    #17

                                    Hi, just guessing, but how do you assign the grid layout into the centralwidget in your mainwindowp.cpp, is it something similar to this:

                                    ...
                                    setCentralWidget(new QWidget());
                                    centralWidget()->setLayout(mainWindowContent->getPageWidgetsLayout());
                                    ...
                                    
                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      Paul Colby
                                      wrote on 2 Jan 2018, 23:42 last edited by
                                      #18

                                      What's the lifetime of your MainWindowContent instance?

                                      It looks to me that MainWindowContent::getPageWidgetsLayout() could be entirely static (if you wanted it to be) except for the this in the connect call. So if, for example, your MainWindowContent instance is destroyed, and re-created later, the slot will have been automatically disconnected, and not reconnected.

                                      Just something else to check...

                                      Cheers.

                                      1 Reply Last reply
                                      1
                                      • W Offline
                                        W Offline
                                        WhatIf
                                        wrote on 3 Jan 2018, 00:38 last edited by
                                        #19

                                        main.cpp

                                        #include "mainwindow.h"
                                        #include <QApplication>
                                        
                                        int main(int argc, char *argv[])
                                        {
                                            QApplication a(argc, argv);
                                            MainWindow w;
                                        
                                            w.buildPage(); 
                                        
                                            w.show();
                                        
                                            return a.exec();
                                        }
                                        

                                        mainwindow.cpp

                                        #include "mainwindow.h"
                                        #include "ui_mainwindow.h"
                                        
                                        #include <QLabel>
                                        #include <QHBoxLayout>
                                        
                                        MainWindow::MainWindow(QWidget *parent) :
                                            QMainWindow(parent),
                                            ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                        }
                                        
                                        MainWindow::~MainWindow()
                                        {
                                            delete ui;
                                        }
                                        
                                        void MainWindow::buildPage()
                                        {
                                            MainWindowContent winContent;
                                        
                                            ui->centralWidget->setLayout(winContent.getPageWidgetsLayout());
                                        }
                                        

                                        mainwindowcontent.cpp

                                        #include "mainwindowcontent.h"
                                        
                                        #include <QDebug>
                                        
                                        MainWindowContent::MainWindowContent(QObject *parent) : QObject(parent)
                                        {
                                        
                                        }
                                        
                                        QGridLayout* MainWindowContent::getPageWidgetsLayout()
                                        {
                                            QGridLayout *gridLayout  =new QGridLayout();
                                        
                                            QLabel *label = new QLabel("");
                                        
                                            QLineEdit *lineEdit = new QLineEdit("Enter Text!");
                                        
                                            QPushButton *button = new QPushButton("OK!");
                                        
                                            QObject::connect(button, SIGNAL (clicked()),this, SLOT (handleButton()));
                                        
                                            gridLayout->addWidget(label,0,0);
                                            gridLayout->addWidget(lineEdit,1,1);
                                            gridLayout->addWidget(button,2,2);
                                           
                                        
                                            return gridLayout;
                                        }
                                        
                                        void MainWindowContent::handleButton()
                                        {
                                            qDebug() << "INSIDE SLOT!";
                                        }
                                        
                                        1 Reply Last reply
                                        0
                                        • P Offline
                                          P Offline
                                          Paul Colby
                                          wrote on 3 Jan 2018, 00:46 last edited by Paul Colby 1 Mar 2018, 00:47
                                          #20

                                          The problem is with the winContent lifetime.

                                          void MainWindow::buildPage()
                                          {
                                              MainWindowContent winContent;
                                          
                                              ui->centralWidget->setLayout(winContent.getPageWidgetsLayout());
                                              // at this point, winContent is destroyed, and automatically disconnected.
                                          }
                                          

                                          You probably want to do something more like:

                                          void MainWindow::buildPage()
                                          {
                                              MainWindowContent * winContent = new MainWindowContent(this);
                                          
                                              ui->centralWidget->setLayout(winContent->getPageWidgetsLayout());
                                          }
                                          

                                          Or move the slot to the MainWindow class.

                                          Cheers.

                                          1 Reply Last reply
                                          4

                                          7/30

                                          2 Jan 2018, 16:20

                                          23 unread
                                          • Login

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