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 member function for call to 'connect' - simple example why doesn't this work??
QtWS25 Last Chance

no matching member function for call to 'connect' - simple example why doesn't this work??

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 4.7k 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.
  • P Offline
    P Offline
    pmh4514
    wrote on 18 Feb 2020, 13:29 last edited by
    #1

    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
    }
    
    
    J A 2 Replies Last reply 18 Feb 2020, 13:35
    0
    • P pmh4514
      18 Feb 2020, 13:29

      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
      }
      
      
      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 18 Feb 2020, 13:35 last edited by J.Hilk
      #2

      @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:

      https://doc.qt.io/qt-5/qspinbox.html#valueChanged


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • P pmh4514
        18 Feb 2020, 13:29

        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
        }
        
        
        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 18 Feb 2020, 13:35 last edited by
        #3

        @pmh4514

        Please post the exact error message with the corresponding code.

        Your problem is most likely, that the spinbox is a local variable which does not outlive the ctor.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bondrusiek
          wrote on 18 Feb 2020, 13:37 last edited by
          #4

          @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);
          

          https://doc.qt.io/qt-5/qspinbox.html#valueChanged

          1 Reply Last reply
          2
          • P Offline
            P Offline
            pmh4514
            wrote on 18 Feb 2020, 13:40 last edited by
            #5

            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!

            J 1 Reply Last reply 18 Feb 2020, 14:08
            2
            • P pmh4514
              18 Feb 2020, 13:40

              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!

              J Offline
              J Offline
              JKSH
              Moderators
              wrote on 18 Feb 2020, 14:08 last edited by
              #6

              @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

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              1

              5/6

              18 Feb 2020, 13:40

              • Login

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