Error when connecting QSpinBox signal to slot



  • 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?



  • @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.



  • @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.



  • @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.



  • @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.



  • @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.



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



  • @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.



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


  • Lifetime Qt Champion

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.