QPathEdit  1.2.2
A Qt-Widget to get local file and folder-paths in an optimized and simple way
qpathedit.h
1 #ifndef QPATHEDIT_H
2 #define QPATHEDIT_H
3 
4 #include <QWidget>
5 #include <QLineEdit>
6 #include <QFileDialog>
7 #include <QIcon>
8 #include <QString>
9 #include <QPointer>
10 
11 #ifdef DESIGNER_PLUGIN
12 #include <QDesignerExportWidget>
13 #define DESIGNER_PLUGIN_EXPORT QDESIGNER_WIDGET_EXPORT
14 #else
15 #define DESIGNER_PLUGIN_EXPORT
16 #endif
17 
18 class QLineEdit;
19 class QCompleter;
20 class PathValidator;
21 class QFileSystemModel;
22 class QToolButton;
23 
25 class DESIGNER_PLUGIN_EXPORT QPathEdit : public QWidget
26 {
27  Q_OBJECT
28 
30  Q_PROPERTY(Style style READ style WRITE setStyle)
32  Q_PROPERTY(QIcon dialogButtonIcon READ dialogButtonIcon WRITE setDialogButtonIcon RESET resetDialogButtonIcon)
34  Q_PROPERTY(PathMode pathMode READ pathMode WRITE setPathMode)
36  Q_PROPERTY(QFileDialog::Options dialogOptions READ dialogOptions WRITE setDialogOptions)
38  Q_PROPERTY(bool editable READ isEditable WRITE setEditable)
40  Q_PROPERTY(bool allowEmptyPath READ isEmptyPathAllowed WRITE setAllowEmptyPath)
42  Q_PROPERTY(bool useCompleter READ useCompleter WRITE setUseCompleter)
44  Q_PROPERTY(QString defaultDirectory READ defaultDirectory WRITE setDefaultDirectory)
46  Q_PROPERTY(QString path READ path WRITE setPath RESET clear NOTIFY pathChanged)
48  Q_PROPERTY(QString placeholder READ placeholder WRITE setPlaceholder)
50  Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters)
52  Q_PROPERTY(QStringList mimeTypeFilters READ mimeTypeFilters WRITE setMimeTypeFilters)
53 
54 public:
56  enum Style {
59  NoButton
60  };
61  Q_ENUM(Style)
62 
63 
64  enum PathMode {
67  AnyFile
68  };
69  Q_ENUM(PathMode)
70 
71 
72  explicit QPathEdit(QWidget *parent = nullptr, Style style = SeperatedButton);
74  explicit QPathEdit(PathMode pathMode, QWidget *parent = nullptr, Style style = SeperatedButton);
76  explicit QPathEdit(PathMode pathMode, QString defaultDirectory, QWidget *parent = nullptr, Style style = SeperatedButton);
77 
79  PathMode pathMode() const;
81  QFileDialog::Options dialogOptions() const;
83  bool isEmptyPathAllowed() const;
85  QString defaultDirectory() const;
87  QString path() const;
89  QUrl pathUrl() const;
91  QString placeholder() const;
93  QStringList nameFilters() const;
95  QStringList mimeTypeFilters() const;
97  bool isEditable() const;
99  bool useCompleter() const;
101  Style style() const;
103  QIcon dialogButtonIcon() const;
104 
106  void setPathMode(PathMode pathMode);
108  void setDialogOptions(QFileDialog::Options dialogOptions);
110  void setAllowEmptyPath(bool allowEmptyPath);
112  void setDefaultDirectory(QString defaultDirectory);
114  bool setPath(QString path, bool allowInvalid = false);
116  void clear();
118  void setPlaceholder(QString placeholder);
120  void setNameFilters(QStringList nameFilters);
122  void setMimeTypeFilters(QStringList mimeTypeFilters);
124  void setEditable(bool editable);
126  void setUseCompleter(bool useCompleter);
128  void setStyle(Style style, QLineEdit::ActionPosition position = QLineEdit::TrailingPosition);
130  void setDialogButtonIcon(const QIcon &icon);
132  void resetDialogButtonIcon();
133 
134 public slots:
136  void showDialog();
137 
138 signals:
140  void pathChanged(QString path);
141 
142 private slots:
143  void updateValidInfo(const QString & path = QString());
144  void editTextUpdate();
145 
146  void dialogFileSelected(const QString & file);
147 
148 private:
149  QLineEdit *edit;
150  QCompleter *pathCompleter;
151  QFileSystemModel *completerModel;
152  PathValidator *pathValidator;
153  QFileDialog *dialog;
154 
155  QString currentValidPath;
156  bool wasPathValid;
157 
158  Style uiStyle;
159  PathMode mode;
160  QString defaultDir;
161  bool allowEmpty;
162 
163  QToolButton *toolButton;
164  QAction *dialogAction;
165  bool hasCustomIcon;
166 
167  QStringList modelFilters(const QStringList &normalFilters);
168  QIcon getDefaultIcon();
169 
170  bool eventFilter(QObject *watched, QEvent *event) override;
171 };
172 
173 #endif // QPATHEDIT_H
A single, existings file. This is basically "Open file".
Definition: qpathedit.h:65
Style
Descibes various styles that the edit can take.
Definition: qpathedit.h:56
The button to open the dialog will be place next to the edit.
Definition: qpathedit.h:57
The button to open the dialog will be placed inside the edit.
Definition: qpathedit.h:58
A single, existing directory. This is basically "Open Folder".
Definition: qpathedit.h:66
PathMode
Describes modes for the kind of path.
Definition: qpathedit.h:64
The QPathEdit provides a simple way to get a path from the user as comfortable as possible...
Definition: qpathedit.h:25