c++ - QFileDialog自定义布局

标签 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

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

最佳答案

在你的情况下,问题是由于你错误地定位了 QDialogBu​​ttonBox,它必须在位置 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

关于c++ - QFileDialog自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48826083/

相关文章:

c++ - 用于 boost::promise<T> 的 Qt 是什么?

javascript - Golang中如何使用javascript获取特定网站元素或提交表单

c++ - 在模板库中输入范围

c++ - 在 C/C++ 中使用带有 GPGME 的特定公钥验证 GPG 签名

c++ - 我是否负责在 QImage::bits() 函数提供的指针上调用 delete?

ruby - Qt::AbstractItemModel::mimeData 中的 Qt/Ruby 应用程序崩溃 - 如何查找并修复原因?

c++ - QTableWidget中如何显示QPushButton被点击的那一行

Qt QSpinBox : How to display uppercase hexadecimal number

c++ - QT Embedded : How to generate an event to ESC (Escape), F1 等按键

c++ - C++中的时差