Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6{
7
9QDir("/home/user/Documents")
10QDir("C:/Users")
12
13
14
15QDir("images/landscape.png")
17
18
19
20QDir("Documents/Letters/Applications").dirName() // "Applications"
21QDir().dirName() // "."
23
24
26QDir directory("Documents/Letters");
27QString path = directory.filePath("contents.txt");
28QString absolutePath = directory.absoluteFilePath("contents.txt");
30
31
33QDir dir("example");
34if (!dir.exists())
35 qWarning("Cannot find the example directory");
37
38
40QDir dir = QDir::root(); // "/"
41if (!dir.cd("tmp")) { // "/tmp"
42 qWarning("Cannot find the \"/tmp\" directory");
43} else {
44 QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt"
46 qWarning("Cannot create the file %s", file.name());
47}
49
50
52QString bin = "/local/bin"; // where /local/bin is a symlink to /usr/bin
53QDir binDir(bin);
54QString canonicalBin = binDir.canonicalPath();
55// canonicalBin now equals "/usr/bin"
56
57QString ls = "/local/bin/ls"; // where ls is the executable "ls"
58QDir lsDir(ls);
59QString canonicalLs = lsDir.canonicalPath();
60// canonicalLS now equals "/usr/bin/ls".
62
63
65QDir dir("/home/bob");
67
68s = dir.relativeFilePath("images/file.jpg"); // s is "images/file.jpg"
69s = dir.relativeFilePath("/home/mary/file.txt"); // s is "../mary/file.txt"
71
72
74QDir::setSearchPaths("icons", QStringList(QDir::homePath() + "/images"));
75QDir::setSearchPaths("docs", QStringList(":/embeddedDocuments"));
76...
77QPixmap pixmap("icons:undo.png"); // will look for undo.png in QDir::homePath() + "/images"
78QFile file("docs:design.odf"); // will look in the :/embeddedDocuments resource path
80
81
83QDir dir("/tmp/root_link");
84dir = dir.canonicalPath();
85if (dir.isRoot())
86 qWarning("It is a root link");
88
89
91// The current directory is "/usr/local"
92QDir d1("/usr/local/bin");
93QDir d2("bin");
94if (d1 == d2)
95 qDebug("They're the same");
97
98
100// The current directory is "/usr/local"
101QDir d1("/usr/local/bin");
102d1.setFilter(QDir::Executable);
103QDir d2("bin");
104if (d1 != d2)
105 qDebug("They differ");
107
108
110C:/Users/Username
112
113
115Q_INIT_RESOURCE(myapp);
117
118
120inline void initMyResource() { Q_INIT_RESOURCE(myapp); }
121
122namespace MyNamespace
123{
124 ...
125
126 void myFunction()
127 {
128 initMyResource();
129 }
130}
132
133
135Q_CLEANUP_RESOURCE(myapp);
137
139QString absolute = "/local/bin";
140QString relative = "local/bin";
141QFileInfo absFile(absolute);
142QFileInfo relFile(relative);
143
145// absFile and relFile now point to the same file
146
147QDir::setCurrent("/tmp");
148// absFile now points to "/local/bin",
149// while relFile points to "/tmp/local/bin"
151}
\inmodule QtCore
Definition qdir.h:20
QString dirName() const
Returns the name of the directory; this is not the same as the path, e.g.
Definition qdir.cpp:715
static void setSearchPaths(const QString &prefix, const QStringList &searchPaths)
Definition qdir.cpp:1109
static bool setCurrent(const QString &path)
Sets the application's current working directory to path.
Definition qdir.cpp:2030
static QDir root()
Returns the root directory.
Definition qdir.h:224
static QString homePath()
Returns the absolute path of the user's home directory.
Definition qdir.cpp:2103
static QString rootPath()
Returns the absolute path of the root directory.
Definition qdir.cpp:2159
@ Executable
Definition qdir.h:31
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
GLdouble s
[6]
Definition qopenglext.h:235
GLsizei const GLchar *const * path
static QString absolutePath(const QString &path)
#define Q_CLEANUP_RESOURCE(name)
Definition qtresource.h:17
#define Q_INIT_RESOURCE(name)
Definition qtresource.h:14
void wrapInFunction()
QFile file
[0]
MyPrototype myFunction
[0]
QDate d1(1995, 5, 17)
[0]
QDate d2(1995, 5, 20)
QString dir
[11]
widget render & pixmap