?? qtemporaryfile.cpp
字號:
\mainclass QTemporaryFile is used to create unique temporary files safely. The file itself is created by calling open(). The name of the temporary file is guaranteed to be unique (i.e., you are guaranteed to not overwrite an existing file), and the file will subsequently be removed upon destruction of the QTemporaryFile object. This is an important technique that avoids data corruption for applications that store data in temporary files. The file name is either auto-generated, or created based on a template, which is passed to QTemporaryFile's constructor. Example: \code { QTemporaryFile file; if (file.open()) { // file.fileName() returns the unique file name } // the QTemporaryFile destructor removes the temporary file } \endcode Reopening a QTemporaryFile after calling close() is safe. For as long as the QTemporaryFile object itself is not destroyed, the unique temporary file will exist and be kept open internally by QTemporaryFile. A temporary file will have some static part of the name and some part that is calculated to be unique. The default filename qt_temp will be placed into the temporary path as returned by QDir::tempPath(). \sa QDir::tempPath(), QFile*/#ifdef QT_NO_QOBJECTQTemporaryFile::QTemporaryFile() : QFile(*new QTemporaryFilePrivate){ Q_D(QTemporaryFile); d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");}QTemporaryFile::QTemporaryFile(const QString &templateName) : QFile(*new QTemporaryFilePrivate){ Q_D(QTemporaryFile); d->templateName = templateName;}#else/*! Constructs a QTemporaryFile in QDir::tempPath(), using the file template "qt_temp.XXXXXX". The file is stored in the system's temporary directory. \sa setFileTemplate(), QDir::tempPath()*/QTemporaryFile::QTemporaryFile() : QFile(*new QTemporaryFilePrivate, 0){ Q_D(QTemporaryFile); d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");}/*! Constructs a QTemporaryFile with a template filename of \a templateName. Upon opening the temporary file this will be used to create a unique filename. If the \a templateName does not contain XXXXXX it will automatically be appended and used as the dynamic portion of the filename. If \a templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct \a templateName if you want use the system's temporary directory. \sa open(), fileTemplate()*/QTemporaryFile::QTemporaryFile(const QString &templateName) : QFile(*new QTemporaryFilePrivate, 0){ setFileTemplate(templateName);}/*! Constructs a QTemporaryFile (with the given \a parent) in QDir::tempPath(), using the file template "qt_temp.XXXXXX". \sa setFileTemplate()*/QTemporaryFile::QTemporaryFile(QObject *parent) : QFile(*new QTemporaryFilePrivate, parent){ Q_D(QTemporaryFile); d->templateName = QDir::tempPath() + QLatin1String("/qt_temp.XXXXXX");}/*! Constructs a QTemporaryFile with a template filename of \a templateName and the specified \a parent. Upon opening the temporary file this will be used to create a unique filename. If the \a templateName does end in XXXXXX it will automatically be appended and used as the dynamic portion of the filename. If \a templateName is a relative path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct \a templateName if you want use the system's temporary directory. \sa open(), fileTemplate()*/QTemporaryFile::QTemporaryFile(const QString &templateName, QObject *parent) : QFile(*new QTemporaryFilePrivate, parent){ setFileTemplate(templateName);}#endif/*! Destroys the temporary file object, the file is automatically closed if necessary and if in auto remove mode it will automatically delete the file. \sa autoRemove()*/QTemporaryFile::~QTemporaryFile(){ Q_D(QTemporaryFile); close(); if (!d->fileName.isEmpty() && d->autoRemove) remove();}/*! \fn bool QTemporaryFile::open() A QTemporaryFile will always be opened in QIODevice::ReadWrite mode, this allows easy access to the data in the file. This function will return true upon success and will set the fileName() to the unique filename used. \sa fileName()*//*! Returns true if the QTemporaryFile is in auto remove mode. Auto-remove mode will automatically delete the filename from disk upon destruction. This makes it very easy to create your QTemporaryFile object on the stack, fill it with data, read from it, and finally on function return it will automatically clean up after itself. Auto-remove is on by default. \sa setAutoRemove(), remove()*/bool QTemporaryFile::autoRemove() const{ Q_D(const QTemporaryFile); return d->autoRemove;}/*! Sets the QTemporaryFile into auto-remove mode if \a b is true. Auto-remove is on by default. \sa autoRemove(), remove()*/void QTemporaryFile::setAutoRemove(bool b){ Q_D(QTemporaryFile); d->autoRemove = b;}/*! Returns the complete unique filename backing the QTemporaryFile object. This string is null before the QTemporaryFile is opened, afterwards it will contain the fileTemplate() plus additional characters to make it unique. \sa fileTemplate()*/QString QTemporaryFile::fileName() const{ if(!isOpen()) return QString(); return fileEngine()->fileName(QAbstractFileEngine::DefaultName);}/*! Returns the set file template. The default file template will be called qt_temp and be placed in QDir::tempPath(). \sa setFileTemplate()*/QString QTemporaryFile::fileTemplate() const{ Q_D(const QTemporaryFile); return d->templateName;}/*! Sets the static portion of the file name to \a name. If the file template ends in XXXXXX that will automatically be replaced with the unique part of the filename, otherwise a filename will be determined automatically based on the static portion specified. If \a name contains a relative file path, the path will be relative to the current working directory. You can use QDir::tempPath() to construct \a name if you want use the system's temporary directory. \sa fileTemplate()*/void QTemporaryFile::setFileTemplate(const QString &name){ Q_ASSERT(!isOpen()); Q_D(QTemporaryFile); fileEngine()->setFileName(name); d->templateName = name;}/*! \fn QTemporaryFile *QTemporaryFile::createLocalFile(const QString &fileName) \overload Works on the given \a fileName rather than an existing QFile object.*//*! Creates and returns a local temporary file whose contents are a copy of the contents of the given \a file.*/QTemporaryFile *QTemporaryFile::createLocalFile(QFile &file){ if (QAbstractFileEngine *engine = file.fileEngine()) { if(engine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::LocalDiskFlag) return 0; //local already //cache bool wasOpen = file.isOpen(); qint64 old_off = 0; if(wasOpen) old_off = file.pos(); else file.open(QIODevice::ReadOnly); //dump data QTemporaryFile *ret = new QTemporaryFile; ret->open(); file.seek(0); char buffer[1024]; while(true) { qint64 len = file.read(buffer, 1024); if(len < 1) break; ret->write(buffer, len); } ret->seek(0); //restore if(wasOpen) file.seek(old_off); else file.close(); //done return ret; } return 0;}/*! \internal*/QAbstractFileEngine *QTemporaryFile::fileEngine() const{ Q_D(const QTemporaryFile); if(!d->fileEngine) d->fileEngine = new QTemporaryFileEngine(d->templateName); return d->fileEngine;}/*! \reimp Creates a unique file name for the temporary file, and opens it. You can get the unique name later by calling fileName(). The file is guaranteed to have been created by this function (i.e., it has never existed before).*/bool QTemporaryFile::open(OpenMode flags){ Q_D(QTemporaryFile); if (!d->fileName.isEmpty()) { setOpenMode(flags); return true; } if (QFile::open(flags)) { d->fileName = d->fileEngine->fileName(QAbstractFileEngine::DefaultName); return true; } return false;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -