Home > Blockchain >  Pass integer argument to slot function for QPushButton
Pass integer argument to slot function for QPushButton

Time:11-01

I'm trying to pass an integer argument to a function using the connect() method in QtCreator.

I'm creating a game where the user will be able to select one of three game setting options. They can click either button 1, button 2, or button 3, and an integer (1, 2, or 3) corresponding to the setting they picked should be passed to the function setDesiredSetting().

I know that it's possible to pass arguments to slots - I believe my problem is similar to this Qt 5 assign slot with parameters to a QPushButton and I tried to use QSignalMapper as the solution suggested.

The issue is, when I go to run my program and I click on button 1 to test, it seems the setDesiredSetting() function never gets called because I don't see the qInfo print statement being printed to the terminal with the desired setting number. Because of this, I can't seem to test if the integer argument was successfully passed.

Code:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QSignalMapper>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

private:
    Ui::MainWindow *ui;

private slots:
    void setDesiredSetting(int desiredSetting);

};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Desired game theme selected
    QSignalMapper mapper;
    connect(ui->themeOneButton, SIGNAL(released()), &mapper, SLOT (map()));
    mapper.setMapping(ui->themeOneButton, 1);
    connect(&mapper, SIGNAL(mapped(int)), this, SLOT (setDesiredSetting(int)));
        
    //connect(ui->themeTwoButton, &QPushButton::clicked, this, &MainWindow::setDesiredSetting(2));
    //connect(ui->themeThreeButton, &QPushButton::clicked, this, &MainWindow::setDesiredSetting(3));

}

void MainWindow::setDesiredSetting(int desiredSetting)
{
    qInfo("Setting selected: %d \n", desiredSetting);
}

CodePudding user response:

First things first, why do you use the old SIGNAL and SLOT macros when you seem to already be aware of the new signal/slot syntax ?

There is no such mapped() signal in QSignalMapper class (so obviously, it cannot work). Maybe QSignalMapper::mappedInt() is what you wanted ?
If you used the proper signal/slot connection syntax instead of the old macros, you would have received a nice compile-time error indicating that the signal you gave doesn't even exist.

In case you didn't know, you can use a lambda function instead of a QSignalMapper, it would make the code much clearer and simpler.

For example:

connect(ui->themeOneButton, &QPushButton::clicked, [&](){
    qInfo() << "Selected setting: " << desiredSetting;
});

Of course the above code sample assumes the desiredSetting variable is accessible in this scope so that the lambda function can get a reference to it. (But since you didn't mention how and where the user choice for the desired settings is made, there might be something more to fix)

Note: if the variable is not guaranteed to be available every time the lambda slot is called, you shall get the value by copy instead of by reference.

  • Related