[SOLVED]Adding dynamically created widgets to layout error
I'm creating an array of widgets and I'm trying to add them to the main layout but I get an error.
Code:
for(int i = 0; i < numLights; i++)
{
colorBoxes[i].setText(description[i]);
ui->mainLayout->addWidget(colorBoxes[i]);
}
It says it can't convert QLabel to QWidget*. It worked before in another project, I have no idea why this is doing it now?
apparently to make it work, I needed to add it as
Code:
ui->mainLayout->addWidget(colorBoxes[i].window());
Re: [SOLVED]Adding dynamically created widgets to layout error
You have an array of type QLabel. QLayout::addWidget() requires a pointer to a widget. QLabel* would be such a pointer.
So you can either change the type of the array or take the addres of each item in the array.
Might even be worth considering usage of a container, e.g. QVector, it is easy to forget to use the delete[] operator when deallocating an array.
Cheers,
_