QFileDialog: is it possible to filter only executables (under Linux)?
if you use native file dialogs some settings have no effect.
This should work:
QFileDialog dlg(this, tr("Select executable"));
dlg.setOption(QFileDialog::DontUseNativeDialog, true);
dlg.setFilter(QDir::Executable | QDir::Files);
Note that this will filer only executables. TO show folders at same time there is no known solution.
How to filter executables using QFileDialog? (Cross-platform solution)
If you want a more personalized filter then you have to use a proxyModel but for this you cannot use the getOpenFileName method since the QFileDialog instance is not easily accessible since it is a static method.
class ExecutableFilterModel(QSortFilterProxyModel):
def filterAcceptsRow(self, source_row, source_index):
if isinstance(self.sourceModel(), QFileSystemModel):
index = self.sourceModel().index(source_row, 0, source_index)
fi = self.sourceModel().fileInfo(index)
return fi.isDir() or fi.isExecutable()
return super().filterAcceptsRow(source_row, source_index)
class OpenSourcePortAction(QAction):
def __init__(self, widget, setSourcePort, config, saveSourcePortPath):
super().__init__("&Open Source Port", widget)
self.widget = widget
self.setShortcut("Ctrl+O")
self.setStatusTip("Select a source port")
self.triggered.connect(self._open)
self.setSourcePort = setSourcePort
self.config = config
self.saveSourcePortPath = saveSourcePortPath
def _open(self):
proxy_model = ExecutableFilterModel()
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
dialog = QFileDialog(
self.widget, "Select a source port", self.config.get("sourcePortDir")
)
dialog.setOptions(options)
dialog.setProxyModel(proxy_model)
if dialog.exec_() == QDialog.Accepted:
filename = dialog.selectedUrls()[0].toLocalFile()
self.saveSourcePortPath(fileName)
self.setSourcePort(fileName)
How to set selected filter on QFileDialog?
Like this:
QString selfilter = tr("JPEG (*.jpg *.jpeg)");
QString fileName = QFileDialog::getOpenFileName(
this,
title,
directory,
tr("All files (*.*);;JPEG (*.jpg *.jpeg);;TIFF (*.tif)" ),
&selfilter
);
The docs are a bit vague about this, so I found this out via guessing.
Filtering in QFileDialog
I believe what you can do is:
- Create a custom proxy model. You can use QSortFilterProxyModel as a base class for your model;
- In the proxy model override the filterAcceptsRow method and return false for files which have the ".backup." word in their names;
- Set new proxy model to the file dialog: QFileDialog::setProxyModel;
Below is an example:
Proxy model:
class FileFilterProxyModel : public QSortFilterProxyModel
{
protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
};
bool FileFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QFileSystemModel* fileModel = qobject_cast<QFileSystemModel*>(sourceModel());
return fileModel->fileName(index0).indexOf(".backup.") < 0;
// uncomment to call the default implementation
//return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
dialog was created this way:
QFileDialog dialog;
dialog.setOption(QFileDialog::DontUseNativeDialog);
dialog.setProxyModel(new FileFilterProxyModel);
dialog.setNameFilter("XML (*.xml)");
dialog.exec();
The proxy model is supported by non-native file dialogs only.
Is it possible to have a QFileDialog allowing either 'files only' or 'directories only'?
I have done some research and with some help from IRC ppl I found an easier solution. Basically adding a widget (checkbox, a suitable for this one) and connecting it to the file dialog does the work.
(It's actually someone else's answer which I have improved & furnished. Thanks to him ;). Posting answer here just for reference if someone else stumbles here).
from sys import argv
from PySide import QtGui, QtCore
class MyDialog(QtGui.QFileDialog):
def __init__(self, parent=None):
super (MyDialog, self).__init__()
self.init_ui()
def init_ui(self):
cb = QtGui.QCheckBox('Select directory')
cb.stateChanged.connect(self.toggle_files_folders)
self.layout().addWidget(cb)
def toggle_files_folders(self, state):
if state == QtCore.Qt.Checked:
self.setFileMode(self.Directory)
self.setOption(self.ShowDirsOnly, True)
else:
self.setFileMode(self.AnyFile)
self.setOption(self.ShowDirsOnly, False)
self.setNameFilter('All files (*)')
def main():
app = QtGui.QApplication(argv)
dialog = MyDialog()
dialog.show()
raise SystemExit(app.exec_())
if __name__ == '__main__':
main()
Preventing file actions in QFileDialog, such as copying, viewing, deleting, etc
The QFileDialog
class already has this functionality:
dialog = QtGui.QFileDialog()
dialog.setOption(QtGui.QFileDialog.ReadOnly, True)
dialog.exec_()
This only seems to work with Qt's built-in file-dialog, though. If you use the static functions to open a native file-dialog, the ReadOnly
option seems to be ignored (I've only tested this on Linux, though).
Related Topics
How to Schedule a Cron for The First Thursday of Every Month
Ansible - Winrm or Requests Is Not Installed
Whats The Easiest Way to Send Messages to My Linux Daemon App
How to Get Wget Only Save File If Its Complete
Linux Mail Adding Content Type Headers Not Working
How to Quickly Check If Linux 'Unzip' Is Installed Using Perl
How to Avoid Sigchld Error in Bash Script That Uses Gnu Parallel
How to Display Nc Return Value in Linux Shell Script
When Is The System Call Set_Tid_Address Used
How to Use Qemu for Learning Arm Linux Kernel Development
How to Install Pysqlite for Python3.4.2
Find Is Returning "Find: .: Permission Denied", But I Am Not Searching In
Windows <Sys/File.H> Equivalent