3

On button press, I am creating a QHBoxLayout and adding three widgets to it (combobox and two spinboxes). I then add the created QHBoxLayout to a vertical layout already defined in Qt Design view.

In another method, I want to access each of these defined QHBoxLayouts and get the values from each of their comboboxes and spinboxes. When iterating over each of the QHBoxLayouts, I am able to see that there are indeed 3 "things" inside of each layout (using count() method), however I am not able to access them and always get an empty result set when trying to find children of the layout.

//In the on click method I am doing the following

QHBoxLayout *newRow = new QHBoxLayout();

QComboBox *animCombo = new QComboBox();
QSpinBox *spinStart = new QSpinBox();
QSpinBox *spinEnd = new QSpinBox();

newRow->addWidget(animCombo);
newRow->addWidget(spinStart);
newRow->addWidget(spinEnd);

ui->animLayout->addLayout(newRow); //animLayout is a vert layout


//in another method, I want to get the values of the widgets in the horiz layouts

foreach( QHBoxLayout *row, horizLayouts ) {

  qDebug() << row->count(); //outputs 3 for each of the QHBoxLayouts

}

Any help is very much appreciated, thanks!

László Papp
  • 51,870
  • 39
  • 111
  • 135
mumush
  • 650
  • 6
  • 14

1 Answers1

1

You could use the following function:

QLayoutItem * QLayout::itemAt(int index) const [pure virtual]

So, I would be writing something like this:

for (int i = 0; i < row.count(); ++i) {
    QWidget *layoutWidget = row.itemAt(i))->widget();
    QSpinBox *spinBox = qobject_cast<QSpinBox*>(layoutWidget);
    if (spinBox)
        qDebug() << "Spinbox value:" << spinBox->value();
    else
        qDebug() << "Combobox value:" << (qobject_cast<QComboBox*>(layoutWidget))->currentText();
}

Disclaimer: this is just pseudo-code to represent the idea.

László Papp
  • 51,870
  • 39
  • 111
  • 135