-1

oplease, help me to solve this probleme. I don't know the proble but if I put QObject in file.h he generate error !

file.h

#include <QMainWindow>
class choice_page_2 : public QMainWindow
{
public:
    choice_page_2();
    QWidget* M_Widget = new QWidget();

public slots:
    
    void On_clicked_delCare();
   
};

#endif // CHOICE_PAGE_2_H

fill.cpp

    choice_page_2::choice_page_2()
{QPushButton *ManageBtn = new QPushButton(tr("Gérer une voiture"));
       QMenu *menu = new QMenu(this);
       QAction* AddCare = new QAction(tr("Ajouter une voiture"), this);
       QAction* DelCare = new QAction(tr("Supprimer une voiture"), this);
      
    QObject::connect( DelCare, SIGNAL(triggered()),this, SLOT(On_clicked_delCare()));
}

I get this error: **QObject::connect: No such slot QMainWindow::On_clicked_delCare()

  • Does this answer your question? [Qt connect "no such slot" when slot definitely does exist](https://stackoverflow.com/questions/10656510/qt-connect-no-such-slot-when-slot-definitely-does-exist) – p-a-o-l-o Jan 31 '22 at 07:38

2 Answers2

0

All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration.

273K
  • 29,503
  • 10
  • 41
  • 64
0

Do not use OLD connection style, use NEW connection style which was introduced in Qt5: https://wiki.qt.io/New_Signal_Slot_Syntax which in your case is:

QObject::connect( DelCare, &QAction::triggered, this, &choice_page_2::On_clicked_delCare);

(you can also remove QObject:: part of connect)

Then not only you can connect to any function or method regardless of whether it is marked as a slot (i.e. your slots will not depend on running MOC - which might be your problem), but you have also compile time checks that the signal and slot arguments match. There are only benefits of this new approach. My advice is to forget that the old style ever existed in Qt4.

PS: I assume you are not using Qt4 any more.