Can not create buttons with for loop
I tried to create a list of buttons using for loop as follow:
Code:
Mywidget::Mywidget()
{
for (int i = 0; i < 5; ++i) {
h_layout->addWidget(close_button);
}
}
But the following error appears:
Code:
error
: no match
for 'operator=' (operand types are
'QPushButton' and
'QPushButton*')close_button
[i
]=new QPushButton("Close",
this);
^
But if i removed and make it only it create the required button, but the problem here that i can not use every pointer to do specific thing for example :
Code:
connect(close_button[2],SIGNAL(clicked(bool)),qApp,SLOT(quit()));
Please help
Re: Can not create buttons with for loop
*sigh*
You really should first learn basic syntax and basic concepts of what is an array, and containers of C++.
Code:
Mywidget::Mywidget()
{
for (int i = 0; i < 5; ++i) {
h_layout->addWidget(pButton);
}
}
Re: Can not create buttons with for loop
This is syntax error in code.
Normally you create a button like so:
Both are single buttons. but if u want many buttons in array you can do something like:
Code:
for (i=0;i<5; i++) {
// maybe good to assign object name too
btnArray
[i
].
objectName = "btn" + QString::number(i
);
}
Re: Can not create buttons with for loop