connect (): how to pass a parameter to the function
- 
I have a program with an array with 9 buttons, I need to link them to a function [check (int idCell)], but I also need to pass a parameter to the function for work it, I have already tried this (it doesn't work): QSignalMapper* signalMapper = new QSignalMapper (this) ; for(i = 0; i < 9; i++) { connect(buttons[i], SIGNAL (clicked()), signalMapper, SLOT (map())); signalMapper->setMapping(buttons[i], i); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(check(int))); }and this too (it doesn't work): for(i = 0; i < 9; i++) { connect(buttons[i], &QAction::triggered, this, [this]{ check(1); }); }help. 
- 
I have a program with an array with 9 buttons, I need to link them to a function [check (int idCell)], but I also need to pass a parameter to the function for work it, I have already tried this (it doesn't work): QSignalMapper* signalMapper = new QSignalMapper (this) ; for(i = 0; i < 9; i++) { connect(buttons[i], SIGNAL (clicked()), signalMapper, SLOT (map())); signalMapper->setMapping(buttons[i], i); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(check(int))); }and this too (it doesn't work): for(i = 0; i < 9; i++) { connect(buttons[i], &QAction::triggered, this, [this]{ check(1); }); }help. @Alby it doesn't work, isn't really an adequate error description. Anyway I assume this would do what you want it to: for(i = 0; i < 9; i++) { connect(buttons[i], &QAction::triggered, this, [=]()->void{ check(i); }); // = -> we capture everything by copy, that includes the value of I during the creation of this connect }
- 
@Alby it doesn't work, isn't really an adequate error description. Anyway I assume this would do what you want it to: for(i = 0; i < 9; i++) { connect(buttons[i], &QAction::triggered, this, [=]()->void{ check(i); }); // = -> we capture everything by copy, that includes the value of I during the creation of this connect }
- 
@J-Hilk Unfortunately it gives me the following error: error: no matching member function for call to 'connect' I forgot to tell you that I run that piece of code inside the builder of a class, if it can help you i can send you all the code 
- 
@Alby said in connect (): how to pass a parameter to the function: @J-Hilk It is QPushButton well, no. 
 But I assume the pointers stored in the list/vector/array are of typeQPushButton*for(i = 0; i < 9; i++) { connect(buttons[i], &QPushButton::clicked, this, [=]()->void{ check(i); }); // = -> we capture everything by copy, that includes the value of I during the creation of this connect }
- 
I have a program with an array with 9 buttons, I need to link them to a function [check (int idCell)], but I also need to pass a parameter to the function for work it, I have already tried this (it doesn't work): QSignalMapper* signalMapper = new QSignalMapper (this) ; for(i = 0; i < 9; i++) { connect(buttons[i], SIGNAL (clicked()), signalMapper, SLOT (map())); signalMapper->setMapping(buttons[i], i); connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(check(int))); }and this too (it doesn't work): for(i = 0; i < 9; i++) { connect(buttons[i], &QAction::triggered, this, [this]{ check(1); }); }help. @Alby said in connect (): how to pass a parameter to the function: connect(buttons[i], &QAction::triggered, this, [this]{ check(1); }); This cannot work, because you have to specify valid lambda function, you have forgotten the parenthesis. For example, this should work: for(i = 0; i < 9; i++) { // capture this and i in lambda to be able to use it connect(buttons[i], &QAction::triggered, this, [this, i](){ check(i); }); }
- 
@Alby said in connect (): how to pass a parameter to the function: @J-Hilk It is QPushButton well, no. 
 But I assume the pointers stored in the list/vector/array are of typeQPushButton*for(i = 0; i < 9; i++) { connect(buttons[i], &QPushButton::clicked, this, [=]()->void{ check(i); }); // = -> we capture everything by copy, that includes the value of I during the creation of this connect }@J-Hilk Yes, I was wrong I meant * QPushButton. I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button: QPushButton * btn = new QPushButton ();I set its geometry and created a connection: connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});With this button, your solution works. So the problem is the button array, which doesn't work, how do I fix it? Other information: the array is instantiated in the class's .h file, like so: QPushButton * buttons [9];and then in the .ccp file, in a method of the same class, I did this: for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { buttons [i] = new QPushButton (grill); buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem. The QFrame is always instantiated in the .h file of the class, like this: QFrame * grill = new QFrame ();
- 
@J-Hilk Yes, I was wrong I meant * QPushButton. I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button: QPushButton * btn = new QPushButton ();I set its geometry and created a connection: connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});With this button, your solution works. So the problem is the button array, which doesn't work, how do I fix it? Other information: the array is instantiated in the class's .h file, like so: QPushButton * buttons [9];and then in the .ccp file, in a method of the same class, I did this: for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { buttons [i] = new QPushButton (grill); buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem. The QFrame is always instantiated in the .h file of the class, like this: QFrame * grill = new QFrame ();@Alby said in connect (): how to pass a parameter to the function: I tried your solution, first with the buttons inside the array, it didn't work again, it didn't work is not a sufficient error description. 
 Whats the compiler error?So the problem is the button array, which doesn't work, how do I fix it? it should work with or without the array, important is only the pointer to the object instance. just in case:
 you can do the following:for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { auto btn = new QPushButton (grill); connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);}); buttons [i] = btn buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }if that doesn't work either, than you're probably mixing your buttons up, and are actually not pressing, the button you're think you're pressing.
 Never mind, reading is a virtue. 
 see @KroMignon 's post, you need to bring your loops into order.
- 
@J-Hilk Yes, I was wrong I meant * QPushButton. I tried your solution, first with the buttons inside the array, it didn't work, so I tried to create a separate button: QPushButton * btn = new QPushButton ();I set its geometry and created a connection: connect (btn, & QPushButton :: clicked, this, [=] () -> void {check (i);});With this button, your solution works. So the problem is the button array, which doesn't work, how do I fix it? Other information: the array is instantiated in the class's .h file, like so: QPushButton * buttons [9];and then in the .ccp file, in a method of the same class, I did this: for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { buttons [i] = new QPushButton (grill); buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }In addition I forgot to mention that the buttons be inside a QFrame as you can see from the code above, I don't know if the QFrame can be the cause of the problem. The QFrame is always instantiated in the .h file of the class, like this: QFrame * grill = new QFrame ();@Alby said in connect (): how to pass a parameter to the function: for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { buttons [i] = new QPushButton (grill); buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }What are you doing here?!? 
 This don't make sense to me.
 Are you aware that you will only have in buttons[] the lastQPushButton(with highest X and Y values). All other instance references are lost.
- 
@Alby said in connect (): how to pass a parameter to the function: for (i = 0; i <9; i ++) { for (y = 0; y <= height * 2; y = y + height) { for (x = 0; x <= width * 2; x = x + width) { buttons [i] = new QPushButton (grill); buttons [i] -> setObjectName ("cell" + QString :: number (i)); buttons [i] -> setGeometry (x, y, width, height); } } }What are you doing here?!? 
 This don't make sense to me.
 Are you aware that you will only have in buttons[] the lastQPushButton(with highest X and Y values). All other instance references are lost.@KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0 I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them. But then how do I create a series of buttons inside an array, without losing the various references to the instance. 
- 
@KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0 I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them. But then how do I create a series of buttons inside an array, without losing the various references to the instance. @Alby said in connect (): how to pass a parameter to the function: I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0 
 I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.
 But then how do I create a series of buttons inside an array, without losing the various references to the instance.Why do you have 3 loop? 
 I can understand loop overxandyto build the matrix, but whyi?
- 
@KroMignon I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0 I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them. But then how do I create a series of buttons inside an array, without losing the various references to the instance. int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); QWidget frame; frame.resize(600,600); const int col = 3; const int row = 3; const int width = frame.width() / col; const int height = frame.width() / row; QList<QPushButton*> buttons; auto check = [](int i)->void{qDebug() << i;}; int i{0}; for(int y{0}; y < row; y++){ for(int x{0}; x < col; x++){ auto btn = new QPushButton(&frame); btn->setGeometry(x * width, y *height, width, height); QObject::connect(btn, &QPushButton::clicked, [=]()->void{check(i);}); buttons.append(btn); i++; } } frame.show(); return app.exec(); }you should seriously consider using a QGridLayout for this. 
- 
@Alby said in connect (): how to pass a parameter to the function: I am creating a small game (tick-tack-toe), those loops I need to create the grid of buttons, where players enter X or 0 
 I honestly did not know, I thought that inside the array, I inserted the various instances of the buttons, without losing them.
 But then how do I create a series of buttons inside an array, without losing the various references to the instance.Why do you have 3 loop? 
 I can understand loop overxandyto build the matrix, but whyi?@KroMignon I realized it now, sorry I was wrong that cycle is not needed 
- 
int main(int argc, char *argv[]) { QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); QWidget frame; frame.resize(600,600); const int col = 3; const int row = 3; const int width = frame.width() / col; const int height = frame.width() / row; QList<QPushButton*> buttons; auto check = [](int i)->void{qDebug() << i;}; int i{0}; for(int y{0}; y < row; y++){ for(int x{0}; x < col; x++){ auto btn = new QPushButton(&frame); btn->setGeometry(x * width, y *height, width, height); QObject::connect(btn, &QPushButton::clicked, [=]()->void{check(i);}); buttons.append(btn); i++; } } frame.show(); return app.exec(); }you should seriously consider using a QGridLayout for this. @J-Hilk I will try to use QGridLayout, the last things () about your code: - 
What is 'auto'? , in the line: 
 auto btn = new QPushButton(&frame);
- 
I saw that you make use of this syntax ex: int y {0}, what is it? 
- 
What does this mean: 
 [] (int i) ?
 in the line:
 auto check = [] (int i) -> void {qDebug () << i;};
- 
What does this mean?: 
 [=] () -> void
 in the line:
 QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});
 Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much 
- 
- 
@J-Hilk I will try to use QGridLayout, the last things () about your code: - 
What is 'auto'? , in the line: 
 auto btn = new QPushButton(&frame);
- 
I saw that you make use of this syntax ex: int y {0}, what is it? 
- 
What does this mean: 
 [] (int i) ?
 in the line:
 auto check = [] (int i) -> void {qDebug () << i;};
- 
What does this mean?: 
 [=] () -> void
 in the line:
 QObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);});
 Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much @Alby said in connect (): how to pass a parameter to the function: Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much don't worry, we all started sometime, somewhere. 
 That said, starting with Qt and c++ in parallel is a hard path, I did it myself, so it's not impossible :D but be warned.What is 'auto'? mmh, if you haven't heard of it, ignore it :P 
 its a place holder for compile time type deduction...
 for more information:
 https://en.cppreference.com/w/cpp/keyword/autoI saw that you make use of this syntax ex: int y {0}, what is it? it is one of the many, many ways to initialise a variable in c++ and the newest one and the one the c++ committee recommends to use. What does this mean: 
 [] (int i) ?
 in the line:
 auto check = [] (int i) -> void {qDebug () << i;};its a lambda, in this case an unnamed function inside a function 
 for more information
 https://en.cppreference.com/w/cpp/language/lambdaQObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);}); an other lambda to call the previous lambda, when the clicked signal is emitted! 🙈 
- 
- 
@Alby said in connect (): how to pass a parameter to the function: Sorry for the many questions I asked, most likely stupid, I'm new to Qt and C ++, in any case thank you very much don't worry, we all started sometime, somewhere. 
 That said, starting with Qt and c++ in parallel is a hard path, I did it myself, so it's not impossible :D but be warned.What is 'auto'? mmh, if you haven't heard of it, ignore it :P 
 its a place holder for compile time type deduction...
 for more information:
 https://en.cppreference.com/w/cpp/keyword/autoI saw that you make use of this syntax ex: int y {0}, what is it? it is one of the many, many ways to initialise a variable in c++ and the newest one and the one the c++ committee recommends to use. What does this mean: 
 [] (int i) ?
 in the line:
 auto check = [] (int i) -> void {qDebug () << i;};its a lambda, in this case an unnamed function inside a function 
 for more information
 https://en.cppreference.com/w/cpp/language/lambdaQObject :: connect (btn, & QPushButton :: clicked, [=] () -> void {check (i);}); an other lambda to call the previous lambda, when the clicked signal is emitted! 🙈 
 

