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!
SIGNAL()
andSLOT()
macros. Instead use the compile-time-checked pointer-to-member-function syntax. It has several advantages: errors are flagged at compile-time rather than run-time so it's harder to make errors and they are more efficient as well. Also; in many cases you don't need the Q_OBJECT macro which sppeds up compilation. – Jesper Juhl