Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
src_gui_dialogs_qfiledialog.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QFileDialog
>
5
#
include
<
QWidget
>
6
7
8
class
FileDialogExample
:
public
QWidget
9
{
10
Q_OBJECT
11
public
:
12
FileDialogExample
();
13
};
14
15
FileDialogExample
::FileDialogExample()
16
{
17
QString fileName;
18
QFileDialog dialog(
this
);
19
{
20
//! [0]
21
fileName = QFileDialog::getOpenFileName(
this
,
22
tr(
"Open Image"
),
"/home/jana"
, tr(
"Image Files (*.png *.jpg *.bmp)"
));
23
//! [0]
24
}
25
26
{
27
//! [1]
28
QFileDialog dialog(
this
);
29
dialog.setNameFilter(tr(
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
));
30
dialog.exec();
31
//! [1]
32
}
33
34
{
35
//! [2]
36
QFileDialog dialog(
this
);
37
dialog.setFileMode(QFileDialog::AnyFile);
38
//! [2]
39
}
40
41
{
42
//! [3]
43
dialog.setNameFilter(tr(
"Images (*.png *.xpm *.jpg)"
));
44
//! [3]
45
}
46
47
{
48
//! [4]
49
dialog.setViewMode(QFileDialog::Detail);
50
//! [4]
51
}
52
53
{
54
//! [5]
55
QStringList fileNames;
56
if
(dialog.exec())
57
fileNames = dialog.selectedFiles();
58
//! [5]
59
}
60
61
{
62
//! [6]
63
dialog.setNameFilter(
"All C++ files (*.cpp *.cc *.C *.cxx *.c++)"
);
64
dialog.setNameFilter(
"*.cpp *.cc *.C *.cxx *.c++"
);
65
//! [6]
66
}
67
68
{
69
//! [7]
70
const
QStringList filters({
"Image files (*.png *.xpm *.jpg)"
,
71
"Text files (*.txt)"
,
72
"Any files (*)"
73
});
74
QFileDialog dialog(
this
);
75
dialog.setNameFilters(filters);
76
dialog.exec();
77
//! [7]
78
}
79
80
{
81
//! [8]
82
QString fileName = QFileDialog::getOpenFileName(
this
, tr(
"Open File"
),
83
"/home"
,
84
tr(
"Images (*.png *.xpm *.jpg)"
));
85
//! [8]
86
}
87
88
{
89
//! [9]
90
QStringList files = QFileDialog::getOpenFileNames(
91
this
,
92
"Select one or more files to open"
,
93
"/home"
,
94
"Images (*.png *.xpm *.jpg)"
);
95
//! [9]
96
}
97
98
{
99
//! [11]
100
QString fileName = QFileDialog::getSaveFileName(
this
, tr(
"Save File"
),
101
"/home/jana/untitled.png"
,
102
tr(
"Images (*.png *.xpm *.jpg)"
));
103
//! [11]
104
}
105
106
{
107
//! [12]
108
QString dir = QFileDialog::getExistingDirectory(
this
, tr(
"Open Directory"
),
109
"/home"
,
110
QFileDialog::ShowDirsOnly
111
| QFileDialog::DontResolveSymlinks);
112
//! [12]
113
}
114
115
{
116
//! [13]
117
QStringList mimeTypeFilters({
"image/jpeg"
,
// will show "JPEG image (*.jpeg *.jpg *.jpe)
118
"image/png"
,
// will show "PNG image (*.png)"
119
"application/octet-stream"
// will show "All files (*)"
120
});
121
122
QFileDialog dialog(
this
);
123
dialog.setMimeTypeFilters(mimeTypeFilters);
124
dialog.exec();
125
//! [13]
126
}
127
128
{
129
/* For convinient quoting
130
//! [14]
131
"Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
132
//! [14]
133
*/
134
}
135
136
{
137
//! [15]
138
auto
fileContentReady = [](
const
QString &fileName,
const
QByteArray &fileContent) {
139
if
(fileName.isEmpty()) {
140
// No file was selected
141
}
else
{
142
// Use fileName and fileContent
143
}
144
};
145
QFileDialog::getOpenFileContent(
"Images (*.png *.xpm *.jpg)"
, fileContentReady);
146
//! [15]
147
}
148
149
{
150
//! [16]
151
QByteArray imageData;
// obtained from e.g. QImage::save()
152
QFileDialog::saveFileContent(imageData,
"myimage.png"
);
153
//! [16]
154
}
155
}
FileDialogExample
Definition
src_gui_dialogs_qfiledialog.cpp:9
qtbase
src
widgets
doc
snippets
code
src_gui_dialogs_qfiledialog.cpp
Generated on
for Qt by
1.18.0