Important: Please read the Qt Code of Conduct -

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 is in the Obsolete Members :)

  • @qcoderpro it looks like documentation is your friend in this case (signal valueChanged is overloaded in QSpinBox)

  • @Pablo-J-Rogina

    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.

  • @qcoderpro
    Actually QSpinBox has two valueChanged signals:

    void valueChanged(int i)
    void valueChanged(const QString &text)

  • @Bonnie

    Where is the QString based one? I only found the int based:

  • @qcoderpro
    It is in the Obsolete Members :)

  • @Bonnie
    Ow, found, thanks, solved.

  • Another connection problem:


    #ifndef DIALOGTEST_H
    #define DIALOGTEST_H
    #include <QDialog>
    class QPushButton;
    class QLineEdit;
    class dialogTest : public QDialog
        dialogTest(QWidget *parent = nullptr);
        QPushButton* button;
        QLineEdit* lineEdit;
    #endif // DIALOGTEST_H


    #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;
       connect(button, &QPushButton::clicked, &QDialog, &QDialog::close);
        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
    The third parameter should be a pointer.
    So "& + ClassName" is not a valid expression.
    You should use "this".

  • @Bonnie

    So an address is not accepted and should it exactly be a pointer!? Why?

    And that this you used is actually a pointer to the object we've created in main.cpp, right?

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

  • @Bonnie

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

  • @qcoderpro
    Yeah, using "this" inside dialogTest equals "&w".

Log in to reply