connect (): how to pass a parameter to the function
-
@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. -
@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.
-
@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 overx
andy
to build the matrix, but whyi
? -
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.
-
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!
🙈