A simple connection error
-
The question is for this connection:
#include <QApplication> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget *window = new QWidget; window->setWindowTitle("Enter Your Age"); QSpinBox *spinBox = new QSpinBox; QSlider *slider = new QSlider(Qt::Horizontal); spinBox->setRange(0, 130); slider->setRange(0, 130); QObject::connect(spinBox, &QSpinBox::valueChanged, slider, &QSlider::setValue); ...
I get this error for the connection, but I cannot understand it well:
error: no matching function for call to 'QObject::connect(QSpinBox&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::)(int))'
QObject::connect(spinBox, &QSpinBox::valueChanged, slider, &QSlider::setValue);
^ -
@qcoderpro it looks like documentation is your friend in this case (signal valueChanged is overloaded in QSpinBox)
-
Don't understand this either!
Here both QSplineBox and QSlider have one valueChanged/setValue respectively with one parameter of type int. Everything looks matched. Can't find the clue for the problem yet. -
Another connection problem:
"dialogTest.h":
#ifndef DIALOGTEST_H #define DIALOGTEST_H #include <QDialog> class QPushButton; class QLineEdit; class dialogTest : public QDialog { Q_OBJECT public: dialogTest(QWidget *parent = nullptr); ~dialogTest(); private: QPushButton* button; QLineEdit* lineEdit; }; #endif // DIALOGTEST_H
"dialogTest.cpp":
#include "dialogtest.h" #include <QPushButton> #include <QLineEdit> #include <QHBoxLayout> #include <QVBoxLayout> dialogTest::dialogTest(QWidget *parent) : QDialog(parent) { button = new QPushButton(tr("Close")); lineEdit = new QLineEdit; QHBoxLayout* hLayout = new QHBoxLayout; hLayout->addWidget(lineEdit); hLayout->addWidget(button); setLayout(hLayout); connect(button, &QPushButton::clicked, &QDialog, &QDialog::close); } dialogTest::~dialogTest() { delete button; delete lineEdit; }
I get this error: error: expected primary-expression before ',' token
connect(button, &QPushButton::clicked, &dialogTest, &dialogTest::close)Even when I use this instead:
connect(button, &QPushButton::clicked, &dialogTest, &dialogTest::close);
I get the same error: error: expected primary-expression before ',' token
connect(button, &QPushButton::clicked, &dialogTest, &dialogTest::close); -
@qcoderpro
An address to an QObject object is acceptable. But I don't think "& + ClassName" is an address. It is not a valid expression at all.
And I haven't seen your main.cpp with a dialogTest, so I'm not sure.
But isn't "this" very basic in C++ class knowledge? -
Yeah, a pointer varies from an address.
This is my main.cpp:
#include "dialogtest.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); dialogTest w; w.show(); return a.exec(); }
So the "this" you used is the pointer to the object, here "w", as the top-level widget of the project. Is it right?