QFileDialog保存打开对话框有两种风格: 在Windows和MacOS X平台上提供本地的对话框风格和Qt自绘的对话框风格

1.打开对话框两种风格

(1)Qt风格

        QFileDialog *fileDialog =new QFileDialog(this);
        fileDialog->setWindowTitle(tr("Open Image"));
        fileDialog->setDirectory(".");
        fileDialog->setFilter(tr("Image Files(*.jpg *.png)"));
        if(fileDialog->exec() == QDialog::Accepted) {
                QString path = fileDialog->selectedFiles()[0];
                QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path);
        }else {
                QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
        }

(2)本地风格
        QString path = QFileDialog::getOpenFileName(this, tr("Open Image"),".", tr("Image Files(*.jpg *.png)"));
        if(path.length() == 0) {
                QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
        }else {
                QMessageBox::information(NULL, tr("Path"), tr("You selected ") + path);
        }

这两种写法虽然功能差别不大,但是弹出的对话框风格却并不一样。getOpenFileName()函数在Windows和MacOS X平台上提供的是本地的对话框,而QFileDialog提供的始终是Qt自己绘制的对话框(还记得前面说过,Qt的组件和Swing类似,也是自己绘制的,而不都是调用系统资源API)。 

2.保存对话框风格

(1)Qt风格

QFileDialog fileDialog;
 fileDialog.setAcceptMode(QFileDialog::AcceptSave);//设置文件对话框为保存模式 
 fileDialog.setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件 
 fileDialog.setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件大小、日期等内容
 fileDialog.setWindowTitle("导出地质分层");
 fileDialog.setDirectory(::GetProjectFolder());
 fileDialog.setFilter("Geos Files(*.geos)");

fileDialog.setOption(QFileDialog::DontConfirmOverwrite);//注意如果不加这句话就会弹出qfiledialog自带的提示是否覆盖的英文对话框,加上这句就不再提示了。

 if(fileDialog.exec() == QDialog::Accepted)
 {
  QString strFilePath = fileDialog.selectedFiles()[0];//得到用户输入的文件名
  QString strFileName = strFilePath + ".geos";
  CObjectArchive ar( strFileName,eStore );

  CGeologicalStratificationRoot* pRoot = dynamic_cast<CGeologicalStratificationRoot*>(GetProject()->GetGeologicalStratificationRoot(true));
  if (pRoot)
  {
   bool bSuccessful = pRoot->Serialize( ar );
   if(bSuccessful) QMessageBox::information(NULL,"提示","导出成功!",QMessageBox::Ok);
   else QMessageBox::information(NULL,"提示","导出失败!",QMessageBox::Ok);
  }
 }

(2)本地风格

   QString strFilePath = QFileDialog::getSaveFileName( NULL,"Save Well log Project",::GetProjectFolder(),
     "well log project file(*.geos)") ;
  if (strFilePath.isEmpty()) return;
 // save project
   CObjectArchive ar( strFilePath,eStore );

 CGeologicalStratificationRoot* pRoot = dynamic_cast<CGeologicalStratificationRoot*>(GetProject()->GetGeologicalStratificationRoot(true));
 if (pRoot)
 {
  pRoot->Serialize( ar );
 }

注意保存对话框:获得的strFilePath的区别:通过getSaveFileName()获得的strName带后缀名geos;通过filedialog.selectedFiles()[0]获得的strName不带后缀名

注意打开对话框:获得的strFilePath的区别:通过fileDialog.selectedFiles()[0]获得的strName带后缀名




你可能感兴趣的:(QFileDialog保存打开对话框有两种风格: 在Windows和MacOS X平台上提供本地的对话框风格和Qt自绘的对话框风格)