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:
#1connect(ui->fontSizeBox, &QSpinBox::valueChanged, this, &MainWindow::changeFontSize);
(This one doesn't compile at all for me, though.)
#2connect(ui->fontSizeBox, SIGNAL(valueChanged(int)), this, SLOT(changeFontSize(int)));
Could anybody explain me the difference between those? -
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. -
@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.
-
The compile error occurs, as the
signal valueChanged()
has an overload forint
and one forQString
.If you use Qt 5.10, you can use:
connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::changeFontSize);
taken from Qt docuFor 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 printsQObject::connect: invalid null parameter
. I will post all the classes I have so far, in case you or someone else can help me out:# # 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.
-
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.
-
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...).
-
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.