Qossai Labyad
Qossai Labyad

Reputation: 79

How to set up a dynamic number of widgets in a dialog?

Can we choose the number of widgets dynamically in a dialog window?

For example I need 2 labels in a dialog, but this number will be determined right before showing the dialog. It'd be passed onto the dialog as an integer argument to a method or a constructor.

What would be some typical ways of doing something like this?

Upvotes: 0

Views: 163

Answers (1)

evilruff
evilruff

Reputation: 4085

Just pass any appropriate parameters to your QDialog-based class constructor and create everything you need there like:

int nNumberOfLabels = 4;
QVBoxLayout * pLayout = new QVBoxLayout();

setLayout( pLayout );

for (int i = 0; i<nNumberOfLabels; i++) {
    QLabel * pLabel = new QLabel();
    pLabel->setText( QString::number(i) );

    pLayout->insertWidget( pLabel );
}

Upvotes: 1

Related Questions