How to connect to a slot with multiple arguments
-
Hi,
I have a slot that I'd like to connect to. The slot contains 2 arguments.
void mySlot(int, int);
I already connected a slot with one argument like this:
QSignalMapper *signalMapper = new QSignalMapper(this); connect(act, SIGNAL(triggered()), signalMapper, SLOT(map())); signalMapper->setMapping(act, ID); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(goToSlot(int)));
but now I would like to connect SLOT(mySlot(int, int))
Is there anyone who knows how to do this?
Thanks in advanced. -
@hobbyProgrammer said in How to connect to a slot with multiple arguments:
Is there anyone who knows how to do this?
And what should the second parameter be?
Using the old signal/slot syntax you can set the second parameter as a default parameter. Using the new signal/slot syntax you can use a lambda. -
@Christian-Ehrlicher Hi,
the first and second arguments are integers.mySlot(int, int);
-
@hobbyProgrammer said in How to connect to a slot with multiple arguments:
the first and second arguments are integers.
I did not ask for the data type, I ask what value you want to pass... and I already gave you the answer on how to achieve it.
-
@Christian-Ehrlicher oh alright. The first argument is an ID that is the result of a QSqlQuery. The second argument is a simple 1, 2 or 3 (depends on which signal is triggered).
-
@Christian-Ehrlicher said in How to connect to a slot with multiple arguments:
Using the old signal/slot syntax you can set the second parameter as a default parameter.
I am using the old signal/slot syntax. But how do I set the second parameter as a default parameter?
Do you have example code? -
@hobbyProgrammer said in How to connect to a slot with multiple arguments:
Do you have example code?
Really C++ basics: https://en.cppreference.com/w/cpp/language/default_arguments
-
MainWindow.h:
private slots: void setStatus(int, int);
MainWindow.cpp:
void MainWindow::connectActions() { connect(value, SIGNAL(statusSafe()), signalMapper, SLOT(map())); signalMapper->setMapping(values, ID); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(setStatus(int, 1))); }
void MainWindow::setStatus(int id, int status) { //some code }
this combination still gives me this error:
QObject::connect: No such slot MainWindow::setStatus(int, 1) in ..\..\..\Documents\QtProject\mainwindow.cpp:67 QObject::connect: (receiver name: 'MainWindow')
-
@hobbyProgrammer Is it so hard reading the links I gave you?
-
@Christian-Ehrlicher yes I don't really seem to understand it.
is this what you meant by setting the default parameter?
void MainWindow::setStatus(int id, int status = 1) { //some code }
because that won't help me at all. I need the status to be variable. But different statusses have different signals so I thought that this might do the trick, but it didn't.
void MainWindow::connectActions() { connect(value, SIGNAL(statusSafe()), signalMapper, SLOT(map())); signalMapper->setMapping(values, ID); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(setStatus(int, 1))); }
-
As I already told you:
Using the new signal/slot syntax you can use a lambda.
C++ basics (again)
-
@hobbyProgrammer said in How to connect to a slot with multiple arguments:
void MainWindow::setStatus(int id, int status = 1)
{
//some code
}As @Christian-Ehrlicher told you, this is a C++ basic knowledge; I think you should start to learn C++ basics this will help you to avoid basic errors.
Default values for arguments are declared in the class declaration:
private slots: void setStatus(int id , int status = 0);
-
Hi,
Where do you get that ID ?
You'll click on the action, get the ID and then call the slot ?