码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-12-17 21:02:09      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

1.打开对话框两种风格

(1)本地风格

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

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

这两种写法虽然功能差别不大,但是弹出的对话框风格却并不一样。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)");

 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自绘的对话框

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://blog.csdn.net/hudfang/article/details/41985235

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!