分类: C/C++
2012-04-06 11:29:09
原文地址:http://blog.csdn.net/zhy282289/article/details/6850326
QFileDialog是使用最高的对话框之一,分打开和保存两类,基本用法简单.
1.基本用法:
QFileDialog w;
w.setAcceptMode(QFileDialog::AcceptOpen); //AcceptOpen打开,AcceptSave保存文件
w.setFileMode(QFileDialog::ExistingFiles);
w.setOption(QFileDialog::ReadOnly, true);
w.setWindowTitle(QString("我叫BOY"));
w.setDirectory(QString("./"));
w.setNameFilter(QString("所有文件(*.*)"));
if (w.exec())
{
}
如果只是基本用法的话,其实直接用静态函数就可以了,你们懂的 QFileDialog::getOpenFileName(this, "boy", "./", "ALL FILE(*.*)");
2.进阶用法:
如果要定制自己的东西就要写多点了,比如改界面上的字,设置左边条有什么,是否可编辑,,默认后缀之类的,注意有四个信号可能会很有用,有两个信号是接受后才触发
直接放代码了汗:
QFileDialog w(this);
connect(&w, SIGNAL(currentChanged(const QString)), this, SLOT(currentChangedSlot(const QString))); //选择改变
connect(&w, SIGNAL(fileSelected(const QString&)), this, SLOT(iamhereSlot())); //文件被选择并接受
w.setAcceptMode(QFileDialog::AcceptOpen); //AcceptOpen打开,AcceptSave保存
w.setFileMode(QFileDialog::ExistingFiles); //显示什么类型文件:文件夹,文件,快捷方式....
w.setOption(QFileDialog::ReadOnly, true); //
w.setWindowTitle(QString("我叫BOY"));
w.setDirectory(QString("./"));
w.setNameFilter(QString("所有文件(*.*);;a(*.*);;b(*.*)"));
w.selectFilter(QString("a(*.*)")); //默认过滤器
//w.setDefaultSuffix(QString("avi")); //保存默认后缀
QListurls;
urls << QUrl::fromLocalFile("./") //我的电脑
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation))
<< QUrl::fromLocalFile("c:/qt");
w.setSidebarUrls(urls); //设置左边框有什么路径
w.setLabelText(QFileDialog::LookIn, QString("查找")); //界面字
...............................
...............................
w.restoreState(m_array); //还原上次状态,只有:布局,路径和历史记录
if (w.exec())
{
QStringList filePaths = w.selectedFiles();
if (!filePaths.isEmpty())
{
for (int i=0;i
{
qDebug() << filePaths[i];
}
}
m_array = w.saveState();
}
三:下面几个典型的师范用法(多文件保存读取)复制代码
- void Dialog::setExistingDirectory()
- { //选择一个文件夹;
- QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
- if (!native->isChecked())
- options |= QFileDialog::DontUseNativeDialog;
- QString directory = QFileDialog::getExistingDirectory(this,
- tr("QFileDialog::getExistingDirectory()"),
- directoryLabel->text(),
- options);
- if (!directory.isEmpty())
- directoryLabel->setText(directory);
- }
复制代码
- void Dialog::setOpenFileName()
- { //打开一个文件;
- QFileDialog::Options options;
- if (!native->isChecked())
- options |= QFileDialog::DontUseNativeDialog;
- QString selectedFilter;
- QString fileName = QFileDialog::getOpenFileName(this,
- tr("QFileDialog::getOpenFileName()"),
- openFileNameLabel->text(),
- tr("All Files (*);;Text Files (*.txt)"),
- &selectedFilter,
- options);
- if (!fileName.isEmpty())
- openFileNameLabel->setText(fileName);
- }
复制代码
- void Dialog::setOpenFileNames()
- { //打开多个文件;
- QFileDialog::Options options;
- if (!native->isChecked())
- options |= QFileDialog::DontUseNativeDialog;
- QString selectedFilter;
- QStringList files = QFileDialog::getOpenFileNames(
- this, tr("QFileDialog::getOpenFileNames()"),
- openFilesPath,
- tr("All Files (*);;Text Files (*.txt)"),
- &selectedFilter,
- options);
- if (files.count()) {
- openFilesPath = files[0];
- openFileNamesLabel->setText(QString("[%1]").arg(files.join(", ")));
- }
- }
- void Dialog::setSaveFileName()
- { //保存一个文件;
- QFileDialog::Options options;
- if (!native->isChecked())
- options |= QFileDialog::DontUseNativeDialog;
- QString selectedFilter;
- QString fileName = QFileDialog::getSaveFileName(this,
- tr("QFileDialog::getSaveFileName()"),
- saveFileNameLabel->text(),
- tr("All Files (*);;Text Files (*.txt)"),
- &selectedFilter,
- options);
- if (!fileName.isEmpty())
- saveFileNameLabel->setText(fileName);
- }
写在后面:
QFileDialog不知道为什么打开这么慢!!!现在也没去研究,知道的朋友求解释求链接啊~~~~