Is it possible to move widgets in QFileDialog's window? Example during opening or saving file. I would like to make a space between file name's line edit and file type's combo box, so I need to move combo box one row lower. Is it possible to make modification like this?
Asked
Active
Viewed 156 times
2
-
2You could give a clearer explanation, some images that illustrate what you want would help us understand you. – eyllanesc Mar 19 '18 at 20:20
-
I added to my post, i marked it in red. I would like to make a space between file name and file type(between line edit and combo box too) – Ieners Mar 19 '18 at 20:35
-
You could show a picture of what you want. – eyllanesc Mar 19 '18 at 20:43
-
Shown, in this space i would like to put an extra widget example new line edit. I know how to add it, but dont know how to modificate window as in the picture – Ieners Mar 19 '18 at 20:57
-
you are going the wrong way asking for adding a space, a more appropriate question is how to insert a QLabel with a QLineEdit in that space that you show. I am right? – eyllanesc Mar 19 '18 at 21:04
-
Yes, you are right, sorry if my question was unclear. I would like to put a widget there – Ieners Mar 19 '18 at 21:11
-
should the widget occupy 1, 2 or 3 columns? considering as 1st column to QLabel, 2nd to QLineEdit and 3rd to QPushButton. – eyllanesc Mar 19 '18 at 21:12
-
I would like to add it in first column – Ieners Mar 19 '18 at 21:15
-
You could show me an image edit what you want, that is, the image with the widget you want to add. – eyllanesc Mar 19 '18 at 21:17
-
If example i would like to copy label(file name), line edit and button "save" to this empty space? So in first column would be label, in second line edit and in third push button. Same as above. You know what i mean or add a picture? – Ieners Mar 19 '18 at 21:21
-
You might set your own view delegate `QFileDialog::setItemDelegate(QAbstractItemDelegate *delegate)` – Mohammad Kanan Mar 20 '18 at 06:24
-
I will try it, thanks for help :) – Ieners Mar 20 '18 at 22:10
-
@MohammadKanan The view delegate has to do with the view embedded in the dialog, not with any other controls. – Kuba hasn't forgotten Monica Mar 21 '18 at 21:53
-
@KubaOber, Hmm .. you mean listings of files ...etc. – Mohammad Kanan Mar 21 '18 at 21:59
-
@MohammadKanan Yes - the file listing is an embedded `QTreeView`. – Kuba hasn't forgotten Monica Mar 21 '18 at 23:33
1 Answers
1
To do this requires modifications to the layout of the widgets that make up the QFileDialog
. A complete example follows. You'd want to add your own content (widgets, layouts, etc.) into the freed-up space - the necessary details are returned from modDialog
. The SO::SpaceButtons
option takes the buttons out of the internal QDialogButtonBox
and lays them out around the inserted row. Otherwise, the space extends only within the first two columns, and the button box remains unchanged.
// filedialog-mod-49371087
#include <QtWidgets>
#include <initializer_list>
struct GridPos {
int row = -1, col = -1, rowSpan = {}, colSpan = {};
GridPos(QGridLayout * layout, QWidget * widget) {
int index = layout->indexOf(widget);
if (index >= 0)
layout->getItemPosition(index, &row, &col, &rowSpan, &colSpan);
}
GridPos(int r, int c, int rs, int cs) : row(r), col(c), rowSpan(rs), colSpan(cs) {}
bool operator==(const GridPos & o) const {
return o.row == row && o.col == col && o.rowSpan == rowSpan && o.colSpan == colSpan;
}
};
namespace SO {
enum ModDialogOption { SpaceButtons = 1 };
Q_DECLARE_FLAGS(ModDialogOptions, ModDialogOption)
}
Q_DECLARE_OPERATORS_FOR_FLAGS(SO::ModDialogOptions)
struct ModDialogResult {
QGridLayout *layout;
int row;
int colSpan;
};
ModDialogResult modDialog(QFileDialog * dialog, SO::ModDialogOptions options = {}) {
auto *layout = qobject_cast<QGridLayout*>(dialog->layout());
auto *fileNameLabel = dialog->findChild<QLabel*>("fileNameLabel");
auto *fileNameEdit = dialog->findChild<QLineEdit*>("fileNameEdit");
auto *buttonBox = dialog->findChild<QDialogButtonBox*>("buttonBox");
auto *fileTypeLabel = dialog->findChild<QLabel*>("fileTypeLabel");
auto *fileTypeCombo = dialog->findChild<QComboBox*>("fileTypeCombo");
auto const buttons = buttonBox->findChildren<QPushButton*>();
// ensure that the dialog has expected layout
Q_ASSERT((GridPos{layout, fileNameLabel} == GridPos{2, 0, 1, 1}));
Q_ASSERT((GridPos{layout, fileNameEdit} == GridPos{2, 1, 1, 1}));
Q_ASSERT((GridPos{layout, buttonBox} == GridPos{2, 2, 2, 1}));
Q_ASSERT((GridPos{layout, fileTypeLabel} == GridPos{3, 0, 1, 1}));
Q_ASSERT((GridPos{layout, fileTypeCombo} == GridPos{3, 1, 1, 1}));
Q_ASSERT(buttons.size() == 2);
// remove widgets and layouts
for (auto *w : std::initializer_list<QWidget*>{buttonBox, fileTypeLabel, fileTypeCombo})
layout->removeWidget(w);
// add widgets in new locations
if (options & SO::SpaceButtons) {
for (auto *b : buttons) b->setParent(dialog);
buttonBox->hide();
layout->addWidget(buttons.at(0), 2, 2, 1, 1);
layout->addWidget(buttons.at(1), 4, 2, 1, 1);
} else {
layout->addWidget(buttonBox, 2, 2, 3, 1);
}
layout->addWidget(fileTypeLabel, 4, 0, 1, 1);
layout->addWidget(fileTypeCombo, 4, 1, 1, 1);
return {layout, 3, 3};
}
int main(int argc, char **argv) {
QApplication app{argc, argv};
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
auto mod = modDialog(&dialog, SO::SpaceButtons);
mod.layout->addItem(new QSpacerItem(0, 50), mod.row, 0);
dialog.show();
return app.exec();
}

Kuba hasn't forgotten Monica
- 95,931
- 16
- 151
- 313