Using connect on a custom QSpinBox
-
Hi. I have the following code, meant to create a custom spin box widget that will round the input value to the nearest multiple of 3 every time the value is changed.
Header:
#ifndef SPINBOXTHREE_H #define SPINBOXTHREE_H #include <QObject> #include <QSpinBox> class spinBoxThree : public QSpinBox { Q_OBJECT public: explicit spinBoxThree(QWidget *parent = nullptr); private slots: int setThree(); }; #endif // SPINBOXTHREE_H
cpp:
#include "spinboxthree.h" spinBoxThree::spinBoxThree(QWidget *parent) : QSpinBox(parent){ connect(this, &QSpinBox::valueChanged, this, [=](){setThree();}); } int spinBoxThree::setThree(){ int input = value(); if (input % 3 != 0){ input = input - (input % 3); } setValue(input); return input; }
I am receiving a "no matching member function" error for the line
connect(this, &QSpinBox::valueChanged, this, [=](){setThree();});
. I am aware that there are issues with using connect on objects not inheriting QObject, but as I need this object to be a QSpinBox, I am wondering whether there are methods I can use to connect the signals and slots within a non-QObject-defined custom class. Please let me know if more information is required. -
@Dummie1138 said in Using connect on a custom QSpinBox:
<unresolved overloaded function type>
That explains the issue: this signal is overloaded, so you will have to do what is shown in documentation: https://doc.qt.io/qt-5/qspinbox.html#valueChanged
-
@Dummie1138 said in Using connect on a custom QSpinBox:
I am aware that there are issues with using connect on objects not inheriting QObject
Not sure what you mean. spinBoxThree is a QSpinBox, which is a QWidget, which is a QObject.
Please post the whole error message. -
@jsulm Thank you for your patience. Here is the error message.
Where I got the understanding of QObject errors from: https://stackoverflow.com/questions/24410130/no-matching-function-for-qobjectconnect
-
@Dummie1138 It seems to come from the code model.
Does the actual build succeed?
You can also put QObject:: in front of connect(). -
@jsulm The build does not succeed. The error messages are:
spinboxthree.cpp:7: error: no matching member function for call to 'connect'
spinboxthree.cpp:7: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'spinboxthree.cpp: In constructor 'spinBoxThree::spinBoxThree(QWidget*)':
spinboxthree.cpp:7:77: error: no matching function for call to 'spinBoxThree::connect(spinBoxThree*, <unresolved overloaded function type>, spinBoxThree*, spinBoxThree::spinBoxThree(QWidget*)::<lambda()>)'
connect(this, &QSpinBox::valueChanged, this, ={setThree();});
^Placing QObject:: in front of connect, like so:
QObject::connect(this, &QSpinBox::valueChanged, this, [=](){setThree();});
resulted in the same error. -
@Dummie1138 said in Using connect on a custom QSpinBox:
<unresolved overloaded function type>
That explains the issue: this signal is overloaded, so you will have to do what is shown in documentation: https://doc.qt.io/qt-5/qspinbox.html#valueChanged