Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

QT signal and slot connection not working

I am making a simple game and want to send a signal from my Game class to my MainWindow. My signal and slot share the same parameter but I can’t connect them. I have tried sending very simple signals with a dummy variable but failed to connect. The code is as follows.

game.h

class Game : public QObject
{
    Q_OBJECT
public:
    Game();
signals:
    void test(int l);

MainWindow.h

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public slots:
    void testSlot(int l);

game.cpp

void someFunction(){
    emit test(2);
}

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
      g{new Game()}

{
    ui->setupUi(this);
    g->gameLoop();
    connect(g,&Game::test,this,&MainWindow::testSlot);

}

How can I get the signals and slots to connect properly? Thank you in advance.

>Solution :

I think the problem may be in the fact that you have g->gameLoop(); BEFORE the connect. If your someFunction is called from the gameLoop, then the connect is performed only after the game has finished and after the execution returns from the gameLoop(). But of course it’s just guessing. I wouldn’t expect to see ‘gameLoop’ called from the Window’s constructor so.. it looks odd as well. Other than that, it looks fine, so if my guess is not correct, then probably the problem lies elsewhere in the code we don’t see.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading