1
votes

I'm developing a file dialog to import file in my application and I want to have an additional QComboBox with a list of formats between File names edit and File of types filter combo box, like this:

enter image description here

I've managed to add QComboBox under filters like this:

enter image description here

using this code:

QGridLayout * layout = qobject_cast <QGridLayout *>(dialog->layout());
QLabel * labelFormat = new QLabel(tr("Format"), dialog);
layout->addWidget(labelFormat, 4, 0);
QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 4, 1);

But I need to swap the last two rows of this grid layout. I've tried something like this to swap rows:

QWidget * w0 = layout->itemAtPosition(3, 0)->widget();
QWidget * w1 = layout->itemAtPosition(3, 1)->widget();
QWidget * w2 = layout->itemAtPosition(3, 2)->widget();

QLabel * labelFormat = new QLabel(tr("Format"), dialog);
layout->addWidget(labelFormat, 3, 0);

QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 3, 1);

layout->replaceWidget(w0, labelFormat);
layout->replaceWidget(w1, comboBoxFormat);

layout->addWidget(w0, 4, 0);
layout->addWidget(w1, 4, 1);
layout->addWidget(w2, 4, 2);

But I got the wrong widgets position:

enter image description here

How can I achieve the widget positioning from the first screenshot?

1

1 Answers

1
votes

In your case the problem is caused by the fact that you are incorrectly locating QDialogButtonBox, this must be in position 3, 2 occupying 2 rows and 1 column:

QGridLayout *layout = qobject_cast<QGridLayout *>(dialog->layout());

QWidget * w0 = layout->itemAtPosition(3, 0)->widget();
QWidget * w1 = layout->itemAtPosition(3, 1)->widget();
QWidget * w2 = layout->itemAtPosition(3, 2)->widget();

QLabel * labelFormat = new QLabel("Format", dialog);
layout->addWidget(labelFormat, 3, 0);

QComboBox * comboBoxFormat = new QComboBox(dialog);
layout->addWidget(comboBoxFormat, 3, 1);

layout->replaceWidget(w0, labelFormat);
layout->replaceWidget(w1, comboBoxFormat);

layout->addWidget(w0, 4, 0);
layout->addWidget(w1, 4, 1);
layout->addWidget(w2, 3, 2, 2, 1);

enter image description here