Congratulations to our 2022 Qt Champions!

Connecting A Signal To A Slot That Has An Int Argument?

  • Hello,

    It's me once again. For those of you who have my past threads, I am approaching the end of my project. I have just hit my final crossroad and I can't seem to figure this out. Here it is:

    I want to connect a QPushButton clicked() signal to an outside Class that takes an integer as a parameter, however I have read already online that I slots and signals must type-match. I looked at QSignalMapper to try and help me with this but I can't seem to figure it out. Here is my code(I am using an MVC-model for my program, GuiDialog is where interaction with user takes place, GameModel is where all of game logic occurs)

    public slots:
    public slots:
    void betOnPlayer(int bet);
    void betOnDealer(int bet);
    void betOnTie(int bet);

    @betToBePassed = slider->value();
    //None of these worked
    connect(ui->betTieButton, SIGNAL(clicked(bool)), gameModel, SLOT(betOnTie(variable));
    connect(ui->betDealerButton, SIGNAL(clicked(bool)), gameModel, SLOT(betOnDealer(variable));
    connect(ui->betPlayerButton, SIGNAL(clicked(bool)), gameModel, SLOT(betOnPlayer(variable));@

    After I found out the above does not work, I tried using QSignalMapper which failed:
    @signalMapper = new QSignalMapper(this);
    connect(ui->betTieButton, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
    connect(ui->betDealerButton, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
    connect(ui->betPlayerButton, SIGNAL(clicked(bool)), signalMapper, SLOT(map()));
    signalMapper->setMapping(ui->betTieButton, gameModel->betOnTie(betToBePassed));
    signalMapper->setMapping(ui->betDealerButton, gameModel->betOnDealer(betToBePassed));
    signalMapper->setMapping(ui->betPlayerButton, gameModel->betOnPlayer(betToBePassed));
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(returnBet(int)));

    int GuiDialog::returnBet(int var)
    betToBePassed = var;
    return betToBePassed;

    Could anyone rear me in the right direction? Thank you for your time.

  • @connect(ui->betTieButton, SIGNAL(clicked(bool)), gameModel, SLOT(betOnTie(variable));@

    What is "variable" supposed to do there? If betOnTie() expects a single parameter of type int, then use:
    @connect(ui->betTieButton, /put signal here/, gameModel, SLOT(betOnTie(int)));@

    Also: You can connect a Signal with parameters to a Slot that takes no parameters. The parameters are simply discarded. But the other way around, it can't work. Which values should be passed in to the Slot function? Furthermore connecting a Signal to a Slot that takes a parameter of different type doesn't make much sense to me. What should be passed? Instead I would suggest to make your Slot compatible to the Signal you want to connect to! Or the other way around: Make your Signal compatible to the Slot you want to send it to.

    betOnTie() obviously expects an integer argument. The QPushButton::clicked() signal does not provide such argument. I don't know how it can make sense to connect those (directly). You could create a Slot like buttonWasClicked() in your GUI dialog and connect your QPushButton's clicked() Signal to that Slot. From this Slot function you could then call gameModel->betOnTie(X), passing whatever value you need to pass...

    connect(ui->betTieButton, SIGNAL(clicked()), this, SLOT(buttonWasClicked()));

    void GuiDialog::buttonWasClicked(void)
    gameModel->betOnTie(42); //replace with value you need to pass ;-)

Log in to reply