0
votes

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!

1
Show Display class.eyllanesc
With modern C++ and recent versions of Qt; I'd advise staying away from the string based signal/slot functions and the related SIGNAL() and SLOT() 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
If you want us to help you, you must provide an minimal reproducible example, your current code needs elements added to make it work.eyllanesc

1 Answers

1
votes

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.