×
Namespaces

Variants
Actions
Revision as of 04:17, 11 October 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to use QDir in Qt

From Nokia Developer Wiki
Jump to: navigation, search

This code snippet demonstrates how to use QDir.

Needs-update.pngThis article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.

Reasons: hamishwillee (09 Feb 2012)
The article uses QDir, which is reasonable, but in the context of a QWidget example, which is not. This should be modified to show QDir in the context of a Qt Quick application or to remove the UI framework around the use of the QDir code.

Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QDir
Created: james1980 (10 Jan 2009)
Last edited: hamishwillee (11 Oct 2012)

Various Function

  • This is used to filter the files that are fetched or saved.
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
  • Sets the sort order used by entryList() and entryInfoList().
dir.setSorting(QDir::Size | QDir::Reversed);
  • The function will create all parent directories necessary to create the directory.
makepath = "d://test";
dir.mkpath(makepath); 

Source Code

#include <QDir>
#include <QWidget>
#include <QVBoxLayout>
#include <QString>
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QDir dir;
QVBoxLayout *layout = new QVBoxLayout;
QWidget *win = new QWidget;
QLabel *label = new QLabel;
QLabel *label1 = new QLabel;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);
QString current,setpath,newdir,newpath,makepath;
makepath = "d://test";
dir.mkdir(hello);
dir.mkpath(makepath); // This command creates all sub folder to make a specific path.
newdir="mydir";
current=dir.currentPath(); // Returns the current path of the application working directory
setpath="d://test";
dir.cdUp();
dir.setPath(setpath); // Sets the path to the setpath string
newpath=dir.currentPath();
dir.mkdir(newdir); // The directory would be created in d://test
label->setText(current);
label1->setText(newpath);
layout->addWidget(label);
layout->addWidget(label1);
win->setLayout(layout);
win->show();
return app.exec();
}


Screenshot

Qdir.JPG

This page was last modified on 11 October 2012, at 04:17.
234 page views in the last 30 days.
×