Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting A Signal To A Slot That Has An Int Argument?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AngelMacias
    wrote on last edited by
    #1

    Hello qt-project.org,

    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)

    GameModel.h
    @//
    public slots:
    public slots:
    void betOnPlayer(int bet);
    void betOnDealer(int bet);
    void betOnTie(int bet);
    /
    /@

    GuiDialog.cpp
    @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:
    GuiDialog.cpp
    @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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      @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...

      @GuiDialog::GuiDialog(void)
      {
      connect(ui->betTieButton, SIGNAL(clicked()), this, SLOT(buttonWasClicked()));
      }

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

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved