QFileDialog自定义布局

时间:2018-02-16 11:45:06

标签: c++ qt qt5 qlayout

我正在开发一个文件对话框来导入我的应用程序中的文件,我希望在QComboBox编辑和File names过滤器组合框之间添加一个File of types格式列表,像这样:

enter image description here

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

enter image description here

使用此代码:

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

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

enter image description here

如何从第一个屏幕截图中实现窗口小部件定位?

1 个答案:

答案 0 :(得分:1)

在您的情况下,问题是由于您错误地找到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);

enter image description here