[Solved] Slot is not recognized

  • Hello,

    I have a small problem with a slot, my connect doesn't work because
    @QObject::connect: No such slot ctrlMachine::MainForm::onAxis_error(QString) in .\ctrlMachine.cpp:94@
    I think that I have to find another way to write my slot, but I also tried with an object, without changes.

    @#include "ctrlMachine.h"

    this->connect(axisCamX, SIGNAL( signalError(QString) ), SLOT(MainForm::onAxis_error(QString)));@
    @#include "mainform.h"

    class ctrlMachine : public QObject {.....etc }; @
    @#include "mainform.h"
    #include "ctrlMachine.h"

    void MainForm::onAxis_error(QString msg)
    QMessageBox msgBox;
    msgBox.setInformativeText( msg );
    @#include "ui_mainform.h"

    class ctrlMachine;

    class MainForm : public QMainWindow

    public slots:
    void onAxis_error(QString msg);


    Ui::MainForm *ui;
    ctrlMachine *machine; ....etc };@
    Is "MainForm::onAxis_error(QString)" right in my slot? Would anyone know how to solve the problem? Thank you in advance

  • Moderators


    There are 2 issues with your connect() statement:

    You forgot to include the pointer to your MainForm object.

    You shouldn't write the "MainForm::" in front of "onAxis_error".

    this->connect(axisCamX, SIGNAL(signalError(QString)),
    mainForm, SLOT(onAxis_error(QString)));

  • On top of what JKSH already said, the MainForm class needs the Q_OBJECT macro.

  • Thank you for your replies! Sorry Zlatomir, the Q_OBJECT is already there, I just forgot to copy it.

    JKSH: Do you mean the pointer this? How can I add it to my MainForm? I don't understand

  • Moderators

    [quote author="silep" date="1397649969"]JKSH: Do you mean the pointer this? How can I add it to my MainForm? I don't understand[/quote]See the code in my previous post. There are 4 arguments (but your code only has 3). The syntax is

    connect(pointerToSender, SIGNAL(nameOfSignal()),
    pointerToReceiver, SLOT(nameOfSlot()));

  • Just to clarify there is an overload of the "old" QObject::connect function that takes only 3 arguments, if the slot is the defined in the same QObject class.

    connect(sender, SIGNAL(...), this, SLOT(...));
    // should be the same as
    connect(sender, SIGNAL(...), SLOT(...));

    As far as I know that only works for the old connect syntax, if you are using Qt5 with function pointers you have to use 4 arguments I think.

  • Thank you, it seems to work now. I thought that the pointer this replaced the mainform pointer, but it is not the case

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.