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 when connecting QSpinBox signal to slot
Forum Updated to NodeBB v4.3 + New Features

Error when connecting QSpinBox signal to slot

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 7.9k 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.
  • E Offline
    E Offline
    El3ctroGh0st
    wrote on last edited by
    #1

    Hello,

    I am trying to connect a signal from QSpinBox to a slot, 'fontSizeBox' being the QSpinBox and inputEdit being a TextEdit widget. This is the code:

    #include "mainwindow.hpp"
    #include "ui_mainwindow.h"
    
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
    
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::changeFontSize(int size)
    {
        qDebug() << QString::number(size);
    
        ui->inputEdit->selectAll();
        ui->inputEdit->setFontPointSize(size);
    }
    

    However, the program does compile but it prints me the error QObject::connect: Cannot connect (null)::valueChanged(int) to MainWindow::changeFontSize(int). Does anybody know how to fix this issue?

    Also, I have another question. So far I've seen two styles of connecting signals and slots:
    #1 connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize); (This one doesn't compile at all for me, though.)
    #2 connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
    Could anybody explain me the difference between those?

    aha_1980A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #10

      Hi,

      Just a wild guess: ui-setupUi(this); should be the first thing you do in the constructor. Only after that are the widgets initialised.

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

      E 1 Reply Last reply
      3
      • E El3ctroGh0st

        Hello,

        I am trying to connect a signal from QSpinBox to a slot, 'fontSizeBox' being the QSpinBox and inputEdit being a TextEdit widget. This is the code:

        #include "mainwindow.hpp"
        #include "ui_mainwindow.h"
        
        #include <QDebug>
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
        
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::changeFontSize(int size)
        {
            qDebug() << QString::number(size);
        
            ui->inputEdit->selectAll();
            ui->inputEdit->setFontPointSize(size);
        }
        

        However, the program does compile but it prints me the error QObject::connect: Cannot connect (null)::valueChanged(int) to MainWindow::changeFontSize(int). Does anybody know how to fix this issue?

        Also, I have another question. So far I've seen two styles of connecting signals and slots:
        #1 connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize); (This one doesn't compile at all for me, though.)
        #2 connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
        Could anybody explain me the difference between those?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @El3ctroGh0st said in Error when connecting QSpinBox signal to slot:

        Also, I have another question. So far I've seen two styles of connecting signals and slots:
        #1 connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize); (This one doesn't compile at all for me, though.)
        #2 connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
        Could anybody explain me the difference between those?

        #1 is the "old-style" connect coming from former Qt versions. It is evaluated at runtime, therefore you get no compile error here but a runtime error.

        #2 is the new-style connect which is already evaluated at compile time. You say you're getting errors here; can you tell us which error? It might give a hint what's going wrong.

        Qt has to stay free or it will die.

        E 1 Reply Last reply
        1
        • aha_1980A aha_1980

          @El3ctroGh0st said in Error when connecting QSpinBox signal to slot:

          Also, I have another question. So far I've seen two styles of connecting signals and slots:
          #1 connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize); (This one doesn't compile at all for me, though.)
          #2 connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
          Could anybody explain me the difference between those?

          #1 is the "old-style" connect coming from former Qt versions. It is evaluated at runtime, therefore you get no compile error here but a runtime error.

          #2 is the new-style connect which is already evaluated at compile time. You say you're getting errors here; can you tell us which error? It might give a hint what's going wrong.

          E Offline
          E Offline
          El3ctroGh0st
          wrote on last edited by El3ctroGh0st
          #3

          @aha_1980 Thanks for answering! `

          error: no matching function for call to 'MainWindow::connect(QSpinBox*&, <unresolved overloaded function type>, MainWindow*, void (MainWindow::*)(int))'
               connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize);
          

          This is the error I'm getting.

          aha_1980A 1 Reply Last reply
          0
          • E El3ctroGh0st

            @aha_1980 Thanks for answering! `

            error: no matching function for call to 'MainWindow::connect(QSpinBox*&, <unresolved overloaded function type>, MainWindow*, void (MainWindow::*)(int))'
                 connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize);
            

            This is the error I'm getting.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #4

            @El3ctroGh0st

            The compile error occurs, as the signal valueChanged() has an overload for int and one for QString.

            If you use Qt 5.10, you can use:

            connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize); taken from Qt docu

            For older Qt versions, there is the 'ugly' version:
            connect(ui->fontSizeBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize);

            If this does not compile, then there is an error with your UI.

            Qt has to stay free or it will die.

            E 1 Reply Last reply
            1
            • aha_1980A aha_1980

              @El3ctroGh0st

              The compile error occurs, as the signal valueChanged() has an overload for int and one for QString.

              If you use Qt 5.10, you can use:

              connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize); taken from Qt docu

              For older Qt versions, there is the 'ugly' version:
              connect(ui->fontSizeBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize);

              If this does not compile, then there is an error with your UI.

              E Offline
              E Offline
              El3ctroGh0st
              wrote on last edited by
              #5

              @aha_1980 Thanks for your help so far! Hm this is weird. This error has been resolved. However, after attemping to compile, I either get a The program has unexpectedly finish error or, if it does run, it prints QObject::connect: invalid null parameter. I will post all the classes I have so far, in case you or someone else can help me out:

              test.pro:

              #
              # Project created by QtCreator 2018-01-06T15:34:50
              #
              #-------------------------------------------------
              
              QT       += core gui
              
              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
              
              TARGET = test
              TEMPLATE = app
              
              
              SOURCES += main.cpp\
                      mainwindow.cpp
              
              HEADERS  += mainwindow.hpp
              
              FORMS    += mainwindow.ui
              

              mainwindow.hpp:

              #ifndef MAINWINDOW_HPP
              #define MAINWINDOW_HPP
              
              #include <QMainWindow>
              
              namespace Ui {
              class MainWindow;
              }
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  explicit MainWindow(QWidget *parent = 0);
                  ~MainWindow();
              
              public slots:
                  void changeFontSize(int);
              
              private:
                  Ui::MainWindow *ui;
              };
              
              #endif // MAINWINDOW_HPP
              

              main.cpp:

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

              mainwindow.cpp:

              #include "mainwindow.hpp"
              #include "ui_mainwindow.h"
              
              #include <QDebug>
              
              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent),
                  ui(new Ui::MainWindow)
              {
                  connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize);
              
                  ui->setupUi(this);
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::changeFontSize(int size)
              {
                  qDebug() << QString::number(size);
              
                  ui->inputEdit->selectAll();
                  ui->inputEdit->setFontPointSize(size);
              }
              

              mainwindow.ui:

              I appreciate any help I can get.

              aha_1980A 1 Reply Last reply
              0
              • E El3ctroGh0st

                @aha_1980 Thanks for your help so far! Hm this is weird. This error has been resolved. However, after attemping to compile, I either get a The program has unexpectedly finish error or, if it does run, it prints QObject::connect: invalid null parameter. I will post all the classes I have so far, in case you or someone else can help me out:

                test.pro:

                #
                # Project created by QtCreator 2018-01-06T15:34:50
                #
                #-------------------------------------------------
                
                QT       += core gui
                
                greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
                
                TARGET = test
                TEMPLATE = app
                
                
                SOURCES += main.cpp\
                        mainwindow.cpp
                
                HEADERS  += mainwindow.hpp
                
                FORMS    += mainwindow.ui
                

                mainwindow.hpp:

                #ifndef MAINWINDOW_HPP
                #define MAINWINDOW_HPP
                
                #include <QMainWindow>
                
                namespace Ui {
                class MainWindow;
                }
                
                class MainWindow : public QMainWindow
                {
                    Q_OBJECT
                
                public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();
                
                public slots:
                    void changeFontSize(int);
                
                private:
                    Ui::MainWindow *ui;
                };
                
                #endif // MAINWINDOW_HPP
                

                main.cpp:

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

                mainwindow.cpp:

                #include "mainwindow.hpp"
                #include "ui_mainwindow.h"
                
                #include <QDebug>
                
                MainWindow::MainWindow(QWidget *parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                {
                    connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize);
                
                    ui->setupUi(this);
                }
                
                MainWindow::~MainWindow()
                {
                    delete ui;
                }
                
                void MainWindow::changeFontSize(int size)
                {
                    qDebug() << QString::number(size);
                
                    ui->inputEdit->selectAll();
                    ui->inputEdit->setFontPointSize(size);
                }
                

                mainwindow.ui:

                I appreciate any help I can get.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @El3ctroGh0st

                I don't see anything strange at a quick glance. I'd recomment you the delete the whole build directory to start a clean rebuild of your project.

                Qt has to stay free or it will die.

                E 1 Reply Last reply
                0
                • aha_1980A aha_1980

                  @El3ctroGh0st

                  I don't see anything strange at a quick glance. I'd recomment you the delete the whole build directory to start a clean rebuild of your project.

                  E Offline
                  E Offline
                  El3ctroGh0st
                  wrote on last edited by
                  #7

                  @aha_1980 Okay, I just tried that. Same result... Program finishes unexpectedly. I seriously have no idea what's going on...

                  aha_1980A 1 Reply Last reply
                  0
                  • E El3ctroGh0st

                    @aha_1980 Okay, I just tried that. Same result... Program finishes unexpectedly. I seriously have no idea what's going on...

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @El3ctroGh0st

                    I just set up a project similar to yours: works as expected. I have the feeling, your UI file is not converted properly to an ui_mainwindow.h or that one is not properly compiled.

                    Delete all build artefacts, close QtCreator, delete all files expect the source, header, UI and .pro file. Asure especially that there are no stray Makefiles hanging around.

                    Qt has to stay free or it will die.

                    E 1 Reply Last reply
                    0
                    • aha_1980A aha_1980

                      @El3ctroGh0st

                      I just set up a project similar to yours: works as expected. I have the feeling, your UI file is not converted properly to an ui_mainwindow.h or that one is not properly compiled.

                      Delete all build artefacts, close QtCreator, delete all files expect the source, header, UI and .pro file. Asure especially that there are no stray Makefiles hanging around.

                      E Offline
                      E Offline
                      El3ctroGh0st
                      wrote on last edited by
                      #9

                      @aha_1980 No success. :/ I've even gone as far as to recreate the program on my laptop (the laptop as well as the downloaded QT Creator app are quite new, which leads me to the conclusion that the problem must be something else...).

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        Hi,

                        Just a wild guess: ui-setupUi(this); should be the first thing you do in the constructor. Only after that are the widgets initialised.

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

                        E 1 Reply Last reply
                        3
                        • SGaistS SGaist

                          Hi,

                          Just a wild guess: ui-setupUi(this); should be the first thing you do in the constructor. Only after that are the widgets initialised.

                          E Offline
                          E Offline
                          El3ctroGh0st
                          wrote on last edited by El3ctroGh0st
                          #11

                          @SGaist Wow... I don't know what to say... This solved the problem, haha. Thanks a lot (to both of you)!

                          1 Reply Last reply
                          1

                          • Login

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