QFileDialog with no edit box
-
Hi,
The following code pops up a dialog window showing File name edit box where selected filename is set. But I don't want to show this edit box.
I want to show only just list of files in the directory.
QFileDialog dialog(this, "Source Directory", filename);
dialog.exec(); -
Try the
QFileDialog::HideNameFilterDetails
option. If it doesn't work, then I believe the only way to modify the file dialog in such a way is to switch to the alien implementation and hide the text box manually. E.g.:// Use alien file dialog QFileDialog dialog(this, QStringLiteral("Source Directory"), filename); dialog.setOption(QFileDialog::DontUseNativeDialog); // Hide the file name edit box QWidget * fileNameEdit = dialog.findChild<QWidget *>(QStringLiteral("fileNameEdit")); Q_ASSERT(fileNameEdit); fileNameEdit->setVisible(false); // ... dialog.exec();
-
@kshegunov
Sorry, but neither works,
The first one just use non-native Filadialog which also has editbox.
The second one crashes. It maybe there is no object named fileNameEdit on QFileDialog class. I tried to find an object name on QFileDialog.h but I could not see any. -
I tried @kshegunov 's way. It is OK! The
fileNameEdit widget
not show. My Qt Version is 5.7.0. -
What version are you using?
-
@kshegunov
I am using Version 5.5.0.
Is it called differently? -
Hi,
To add to @kshegunov, I'd search for QLineEdit rather than QWidget, that should narrow things a bit.
-
@SGaist
Still the same.
If fileNameEdit is a name of object used in the library, how can I find it in source code. -
It's in qfiledialog.ui in Qt's sources.
On which OS are you running that ?
-
@samdol said in QFileDialog with no edit box:
I am using Version 5.5.0.
Are you sure? @SGaist got ahead of me, but what OS are you running?
Is it called differently?
Not as far as I can see. You can check it yourself:
http://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/dialogs/qfiledialog.ui?h=v5.5.0#n287@SGaist said in QFileDialog with no edit box:
I'd search for QLineEdit rather than QWidget, that should narrow things a bit.
Ordinarily I'd agree, however here it's a search by name and there's no gain in casting to line edit.
-
@kshegunov
I installed Qt 5.5.0 by
qt-opensource-windows-x86-mingw492-5.5.0.exe
I could not see qfiledialog.ui because, I think, it is not source code.
If fileNameEdit is the correct name, I don't understand why it crashes. -
@samdol said in QFileDialog with no edit box:
If fileNameEdit is the correct name, I don't understand why it crashes
Probably because fileNameEdit is nullptr? This is easy to check.
-
@samdol said in QFileDialog with no edit box:
If fileNameEdit is the correct name, I don't understand why it crashes.
By "crashes" you probably mean you trip the assertion I've put. Why I don't know for sure, you should debug it and check against your sources (if those are not available, building Qt so you obtain a source tree corresponding to the binary, might be a good idea).
-
Just a silly idea: you didn't remove
dialog.setOption(QFileDialog::DontUseNativeDialog);
from your code, did you ?If you are trying that with the native dialog it won't work.
-
@SGaist
Thank you SGaist and kshegunov,
I was silly, I should have put QFileDialog::DontUseNativeDialog.
Now with the following, I could hide fileNameEdit, filenameLabel and
buttonBox. It looks much better now. By the way, do we need QStringLiteral?QFileDialog dialog(this, "Source Directory", directory); dialog.setNameFilter(myFilter); dialog.setOption(QFileDialog::DontUseNativeDialog); QWidget * fileNameEdit = dialog.findChild<QWidget *>("fileNameEdit"); //QWidget * fileNameEdit = dialog.findChild<QWidget *>(QStringLiteral("fileNameEdit")); Q_ASSERT(fileNameEdit); fileNameEdit->setVisible(false); QWidget * fileNameLabel = dialog.findChild<QWidget *>("fileNameLabel"); fileNameLabel->setVisible(false); QWidget * buttonBox = dialog.findChild<QWidget *>("buttonBox"); buttonBox->setVisible(false); dialog.exec();
'''
-
@samdol said in QFileDialog with no edit box:
I was silly, I should have put QFileDialog::DontUseNativeDialog.
Yes, you must use the alien dialog if you want to manipulate it in such a way.
By the way, do we need QStringLiteral?
It's a macro that enables some string literal optimizations if your compiler supports them otherwise it defaults to
QString("text")
. I advise using it, although it's not strictly necessary.