QFileDialog自定义布局

QFileDialog custom layout

我正在开发一个文件对话框以在我的应用程序中导入文件,我想要一个额外的 QComboBox,其中包含 File names 编辑和 File of types 过滤器组合之间的格式列表盒子,像这样:

我已经设法在这样的过滤器下添加 QComboBox

使用此代码:

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);

但我需要交换此网格布局的最后两行。我试过这样的方法来交换行:

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);

但是我得到了错误的小部件位置:

如何实现第一个截图的小部件定位?

在你的情况下,问题是由于你错误定位QDialogButtonBox,这必须在位置3,2占据2行1列:

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);