0
votes

My aim is to create an array of command link buttons dynamically on clicking a push button in and then display them all inside a vertical layout inside a QscrollArea. I get the data for the buttons from a database. For this I created a slot for the button and wrote the following code inside the slot function.

QCommandLinkButton *slotButtons[10];
for(int i=0; slotQuery.next(); i++)
{
    slotButtons[i] = new QCommandLinkButton;
    slotButtons[i]->setText(slotQuery.value(0).toString());
    slotButtons[i]->setDescription(slotQuery.value(1).toString());


    ui->scrollAreaSlots->layout()->addWidget(slotButtons[i]);
    ui->scrollAreaSlots->show();
    slotButtons[i]->show();


} 

This compiles without errors but the buttons are not visible, even after calling show. Could anyone tell me where I'm going wrong?

Update: If i remove all the "[i]"s and comment the loop; basically creating just a single command link button, it works perfectly. But it doesn't work for the loop. Is everything right with my looping?

2

2 Answers

1
votes

The QScrollArea has one child widget which can contain other widgets.

When a QScrollArea widget is created with Qt Creator's UI designer, Qt Creator creates automatically a widget named scrollAreaWidgetContents. Buttons are then added to that widget's layout, which is not created automatically. The layout is created in the following code which also add the buttons:

QCommandLinkButton *slotButtons[10];
QVBoxLayout* layout = new QVBoxLayout(ui->scrollAreaWidgetContents);
for(int i=0; slotQuery.next(); i++)
{
    slotButtons[i] = new QCommandLinkButton;
    slotButtons[i]->setText(slotQuery.value(0).toString());
    slotButtons[i]->setDescription(slotQuery.value(1).toString());
    ui->scrollAreaWidgetContents->layout()->addWidget(slotButtons[i]);
} 
0
votes

Try to add the following line (before adding the button to the scroll area)

slotButtons[i]->setVisible(true);

This should make sure that button itself is visible when you add it to the scrool area.