QFileDialog的使用

来源:互联网 发布:网络通信概念股 编辑:程序博客网 时间:2024/05/16 16:21

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"));  //保存默认后缀
QList<QUrl> urls;
 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<filePaths.size();i++)
{
qDebug() << filePaths[i];
}
}
m_array = w.saveState();
}




写在后面:

QFileDialog不知道为什么打开这么慢!!!现在也没去研究,知道的朋友求解释求链接啊~~~~