QT file read and write operations Notes

Make notes about this part of the simple things in mind about

 

 

 

 

Standard operating system usually provides a series of dialog boxes, such as file selection, font selection, color selection, these standard dialog box provides a consistent look and feel for the application layer sequence. Qt for these standard dialog defines related classes, such as: QFileDialog, QFontDialog, the QColorDialog, QInputDialog, QMessageBox, QPrintDialog, QErrorMessage, QProgressDialog etc.

 

 

QFileDialog

QFileDialog class is a standard File Open and Save dialog boxes. QFileDialog class inherits QDialog class;

QFileDialog text filter used when opening a file, the specified file extension for display. You can also set the starting directory and file specified extension when using QFileDialog open the file.

 

QFileDialog using two methods, one is relatively simple using the "static function method" and the other is to customize the various details of the "constructor Law"

Official website Manual:

 

QString    getExistingDirectory(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), Options options = ShowDirsOnly)
QUrl    getExistingDirectoryUrl(QWidget * parent = 0, const QString & caption = QString(), const QUrl & dir = QUrl(), Options options = ShowDirsOnly, const QStringList & supportedSchemes = QStringList())
QString    getOpenFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
QStringList    getOpenFileNames(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
QUrl    getOpenFileUrl(QWidget * parent = 0, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0, const QStringList & supportedSchemes = QStringList())
QList<QUrl>    getOpenFileUrls(QWidget * parent = 0, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0, const QStringList & supportedSchemes = QStringList())
QString    getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)
QUrl    getSaveFileUrl(QWidget * parent = 0, const QString & caption = QString(), const QUrl & dir = QUrl(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0, const QStringList & supportedSchemes = QStringList())

 

 

 

function:

getOpenFileName

For example, I use here in getOpenFileName triggered () in

 

 
 

#include "mainwindow.h"
#include "ui_mainwindow.h"

 
 

#include <QTextCodec>
#include <QFileDialog>
#include <QString>
#include <QDebug>
#include <QByteArray>
#include <string.h>
#define cout qDebug()




void
MainWindow::on_actiondakai_2_triggered() { QString fileName = QFileDialog::getOpenFileName(); cout<<"====="<<fileName<<"========"; }

 

 

 

 

 

 

 

getExistingDirectory

void MainWindow::on_actiondakai_2_triggered()
{
//    QString fileName = QFileDialog::getOpenFileName();
    QString fileName = QFileDialog::getExistingDirectory(NULL,"caption",".");
     cout<<"====="<<fileName<<"========";

}

 

 

 

Other similar method is no longer described.

 

getExistingDirectoryUrl
and getExistingDirectory similar function also get folder path, the biggest difference is that the parameters in the path QUrl type, which provides the ability to select the remote directory for the user.

 

getOpenFileUrl

Get Remote File
provides users with the ability to get remote file path

 

getOpenFileUrls

Obtaining a plurality of remote files
can be multiple remote file path

 

getOpenFileNames

Get more local files

 

 

getSaveFileName

Save a local file dialog

 

getSaveFileUrl

Save Remote File dialog box

 

 

 

 

 

 

 

 

In addition to the static function can also facilitate custom constructor to open the file details

Construct a QFileDialog objects:

QFileDialog fd;

 

Is set to open or save a file dialog AcceptMode

fd.setAcceptMode (QFileDialog :: AcceptOpen); // File dialog box open type 

fd.setAcceptMode (QFileDialog :: AcceptSave); // File dialog box for Save as type

 

 

The level of detail settings dialog box is displayed

fd.setViewMode (QFileDialog :: the Detail); // details 
fd.setViewMode (QFileDialog :: List); // only list

 

 

Settings dialog box returns

fd.setFileMode (QFileDialog :: anyfile); // file name, regardless of whether there 

fd.setFileMode (QFileDialog :: ExistingFile); // exists, a single file name 

fd.setFileMode (QFileDialog :: Directory); // Folder name 

fd.setFileMode (QFileDialog :: ExistingFiles); // multi-file

 

 

Set Title

fd.setWindowTitle ( " title " );

 

Set the default suffix

fd.setDefaultSuffix("txt");

 

 

 

Gets the directory address dialog

QDir you fd.directory = (); qDebug () << you;

 

 

Returns the file icon provider 

QFileIconProvider * QFileDialog::iconProvider() const 

 

 

Set the history directory setHistory

    DirList QStringList; 
    DirList << " C: \\ Intel " ; // add a path 
 
    fileDlg -> setHistory (DirList);

 

Guess you like

Origin www.cnblogs.com/-qing-/p/11570492.html