PDA

View Full Version : [SOLVED]Adding dynamically created widgets to layout error



bnosam
7th April 2015, 02:41
I'm creating an array of widgets and I'm trying to add them to the main layout but I get an error.



QLabel *colorBoxes = new QLabel[numLights];
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

ui->mainLayout->addWidget(colorBoxes[i].window());

anda_skoa
7th April 2015, 07:05
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,
_