Hilton Khadka
Hilton Khadka

Reputation: 123

QToolButton signal and slot

I have a display.cpp class, It has a QToolButton "start" that should starts a game. I also have a singleton controller.cpp class, that creates an object of display.cpp and get's the pointer of the QToolButton "start". So now i tried to connect the "start" button's clicked(bool) SIGNAL to my custom SLOT in controller.h, it doesn't work for some reason.

It does compile and run.

Both class do inherit QObject and also Q_Object macro.

//controller.h
public slots:
    void initGame(bool);

//controller.cpp
void Controller::initConnectors(){

    /*create object of Display.cpp*/
    Display *sender = new Display();

    /*getStartButton from display.cpp and connect it*/
    connect(sender->getStartButton(),SIGNAL(clicked(bool)), getControllersInstance() ,SLOT(initGame(bool)));
}

void Controller::initGame(bool a)
{
  std::cout << "Received Signal?" <<std::endl;
}

The display.cpp:

/*Button that is used to start the game*/
QToolButton *Display::getStartButton() const
{
return startButton;
}

And in the main.cpp:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow m;
Controller::Create();
Controller * controller = Controller::getControllerInstance();
controller->initConnectors();
m.show();
return app.exec();
}

After pressing the "start" button, nothing happens! I tested the "start" button in the display.cpp class itself, and there it works fine. But after i pass the button to controller.cpp, it does't work anymore!

Upvotes: 0

Views: 632

Answers (1)

wolff
wolff

Reputation: 436

You declare the slot as int initGame(bool);, but define void initGame(bool) later.

I guess this is the source of the problem here. Make sure to use the same types. If this does not solve the problem, we will need to see the full code.

Upvotes: 1

Related Questions