Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_io_qdir.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 <QDir>
5#include <QPixmap>
6
8{
9
10 {
11 //! [0]
12 QDir("/home/user/Documents");
13 QDir("C:/Users");
14 //! [0]
15 }
16
17 {
18 //! [1]
19 QDir("images/landscape.png");
20 //! [1]
21 }
22
23 {
24 //! [2]
25 QDir("Documents/Letters/Applications").dirName(); // "Applications"
26 QDir().dirName(); // "."
27 //! [2]
28 }
29
30 {
31 //! [3]
32 QDir directory("Documents/Letters");
33 QString path = directory.filePath("contents.txt");
34 QString absolutePath = directory.absoluteFilePath("contents.txt");
35 //! [3]
36 }
37
38 {
39 //! [4]
40 QDir dir("example");
41 if (!dir.exists())
42 qWarning("Cannot find the example directory");
43 //! [4]
44 }
45
46 {
47 //! [5]
48 QDir dir = QDir::root(); // "/"
49 if (!dir.cd("tmp")) { // "/tmp"
50 qWarning("Cannot find the \"/tmp\" directory");
51 } else {
52 QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt"
53 if (!file.open(QIODevice::ReadWrite))
54 qWarning("Cannot create the file %s", qPrintable(file.fileName()));
55 }
56 //! [5]
57 }
58
59 {
60 //! [6]
61 QString bin = "/local/bin"; // where /local/bin is a symlink to /usr/bin
62 QDir binDir(bin);
63 QString canonicalBin = binDir.canonicalPath();
64 // canonicalBin now equals "/usr/bin"
65
66 QString ls = "/local/bin/ls"; // where ls is the executable "ls"
67 QDir lsDir(ls);
68 QString canonicalLs = lsDir.canonicalPath();
69 // canonicalLS now equals "/usr/bin/ls".
70 //! [6]
71 }
72
73 {
74 //! [7]
75 QDir dir("/home/bob");
76 QString s;
77
78 s = dir.relativeFilePath("images/file.jpg"); // s is "images/file.jpg"
79 s = dir.relativeFilePath("/home/mary/file.txt"); // s is "../mary/file.txt"
80 //! [7]
81 }
82
83 {
84 //! [8]
85 QDir::setSearchPaths("icons", QStringList(QDir::homePath() + "/images"));
86 QDir::setSearchPaths("docs", QStringList(":/embeddedDocuments"));
87 //...
88 QPixmap pixmap("icons:undo.png"); // will look for undo.png in QDir::homePath() + "/images"
89 QFile file("docs:design.odf"); // will look in the :/embeddedDocuments resource path
90 //! [8]
91 }
92
93 {
94 //! [9]
95 QDir dir("/tmp/root_link");
96 dir = dir.canonicalPath();
97 if (dir.isRoot())
98 qWarning("It is a root link");
99 //! [9]
100 }
101
102 {
103 //! [10]
104 // The current directory is "/usr/local"
105 QDir d1("/usr/local/bin");
106 QDir d2("bin");
107 if (d1 == d2)
108 qDebug("They're the same");
109 //! [10]
110 }
111
112 {
113 //! [11]
114 // The current directory is "/usr/local"
115 QDir d1("/usr/local/bin");
116 d1.setFilter(QDir::Executable);
117 QDir d2("bin");
118 if (d1 != d2)
119 qDebug("They differ");
120 //! [11]
121 }
122
123#if TEXT_FILE
124 //! [12]
125 C:/Users/Username
126 //! [12]
127#endif
128}
129
130#if 0
131//! [13]
132Q_INIT_RESOURCE(myapp);
133//! [13]
134#endif
135
136//! [14]
137inline void initMyResource() { Q_INIT_RESOURCE(myapp); }
138
139namespace MyNamespace
140{
141 //...
142
144 {
146 }
147}
148//! [14]
149
150# if 0
151//! [15]
152Q_CLEANUP_RESOURCE(myapp);
153//! [15]
154#endif
155
157{
158 //! [16]
159 QString absolute = "/local/bin";
160 QString relative = "local/bin";
161 QFileInfo absFile(absolute);
162 QFileInfo relFile(relative);
163
164 QDir::setCurrent(QDir::rootPath());
165 // absFile and relFile now point to the same file
166
167 QDir::setCurrent("/tmp");
168 // absFile now points to "/local/bin",
169 // while relFile points to "/tmp/local/bin"
170 //! [16]
171}
void wrapInFunction()
[16]
void initMyResource()
[14]