Signal Slot Mechanism Error
Solved
General and Desktop
-
Hello all,
I would like to make signal slot mechanism between two seperate classes. I have created "Bicycle" and "Car" classes. Here the codes.
#include "mainwindow.h" #include "ui_mainwindow.h" #include "car.h" #include "bicycle.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ; car = new Car(); bicycle = new Bicycle(); connect(bicycle,&Bicycle::bicycleSignal(),car,&Car::carSlot()); } MainWindow::~MainWindow() { delete ui; }
Classes are defined like below.
#ifndef CAR_H #define CAR_H #include <QObject> class Car : public QObject { Q_OBJECT public: explicit Car(QObject *parent = nullptr); signals: void carSignal(); public slots: void carSlot(); }; #endif // CAR_H
#ifndef BICYCLE_H #define BICYCLE_H #include <QObject> class Bicycle : public QObject { Q_OBJECT public: explicit Bicycle(QObject *parent = nullptr); signals: void bicycleSignal(); public slots: void bicycleSlot(); }; #endif // BICYCLE_H
I have created the instances belong to Car and Bicycle classes. I got an error like " cannot call member function ‘void Bicycle::bicycleSignal()’ without object
connect(bicycle,&Bicycle::bicycleSignal(),car,&Car::carSlot());
^"How can I solve this issue?
-
connect(bicycle,&Bicycle::bicycleSignal(),car,&Car::carSlot());
Just by reading what the syntax for
connect()
tells/shows you what to write for the signal & slot methods. It does not tell you to put trailing()
on those function references.