0

This is the full error i am getting, I do have the moc file in my build folder I am trying to debug and this message apears

Debugging starts
    QObject::connect: No such slot MainWindow::numberClicked(QString buttonInput) in ..\calculator\mainwindow.cpp:14
    QObject::connect:  (sender name:   'pushButtonNr0')
    QObject::connect:  (receiver name: 'MainWindow')

This is my header file, i do have Q_Object macro like some others suggest

    class MainWindow : public QMainWindow {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

    private slots:

        void numberClicked(QString buttonInput);

This is my cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    _ui(new Ui::MainWindow) {
    _ui->setupUi(this);

    _inputSwitchDen = true;

    connect(_ui->pushButtonNr0, SIGNAL(clicked()), this, SLOT(numberClicked(QString buttonInput)));

and this is the function

void MainWindow::numberClicked(QString buttonInput = "") {
    QPushButton *clickedButton = qobject_cast<QPushButton *> (sender());
    if (buttonInput == "") {
    buttonInput = clickedButton->text();
    }


    _ui->labelMessage->setText("");


    if (_inputSwitchDen) {
    if (_ui->lineDen->text() == "1")
        _ui->lineDen->setText(buttonInput);
    else {
        _ui->lineDen->setText(_ui->lineDen->text() + buttonInput);
    }
    } else {
    if (_ui->lineNum->text() == "0") {
        _ui->lineNum->setText(buttonInput);

    } else {
        _ui->lineNum->setText(_ui->lineNum->text() + buttonInput);
    }
    }

Thanks for reading

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
rednefed
  • 71
  • 2
  • 11
  • Hint: prefer the compile-time-checked pointer-to-member-function `connect()` syntax over the string based one (the one using the `SIGNAL()` and `SLOT()` macros). The new syntax is both safer and faster. – Jesper Juhl Mar 10 '18 at 13:55
  • do you have an example? – rednefed Mar 10 '18 at 13:58
  • For the new signal/slot syntax see the [documentation](http://doc.qt.io/qt-5/signalsandslots.html) and [wiki](https://wiki.qt.io/New_Signal_Slot_Syntax). – G.M. Mar 10 '18 at 14:35

2 Answers2

1

You should make use of the new signal/slot syntax.

If you really can't change the declaration of MainWindow::numberClicked (although overloading it would be the obvious solution) then you can probably just use a lambda (untested)...

connect(_ui->pushButtonNr0, &QPushButton::clicked, this,
        [this]()
        {
          numberClicked();
        });

Note, also, that when using the new syntax there is no need to declare slots explicitly -- they're just normal functions.

G.M.
  • 12,232
  • 2
  • 15
  • 18
  • thanks, this actually worked, but i still had to add and empty string to the function like this numberClicked(""); very weird. – rednefed Mar 11 '18 at 06:47
0

The row

connect(_ui->pushButtonNr0, SIGNAL(clicked()), this, SLOT(numberClicked(QString buttonInput)));

shlould be

connect(_ui->pushButtonNr0, SIGNAL(clicked()), this, SLOT(numberClicked()));

connect(_ui->pushButtonNr0, SIGNAL(clicked()), this, SLOT(numberClicked(QString buttonInput)));

without "QString buttonInput" because the click signal can't pass an argument.

If you don't want to change your function you can overload it.

void numberClicked() {
    numberClicked("");
}

void numberClicked(QString buttonInput) {
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • i tried that before making this thread, and i had the same error, thanks anyways – rednefed Mar 10 '18 at 14:16
  • Note that in addition to what is stated in this answer you also need to change the declaration of `numberClicked` to `void numberClicked()`. – G.M. Mar 10 '18 at 14:19
  • cant really remove the parameter, i need it in some other part of the programs – rednefed Mar 10 '18 at 14:59