c++ - 如何检索具有完整路径的文件名以将数据写入其中

标签 c++ qt qfile

简要说明:在我的 Qt 实用程序中,我希望在用户点击关闭按钮后发生以下事情

  • 1) 出现一个文件对话框,其中包含保存/取消选项和 默认文件名。

  • 2) 如果用户将文件保存在他的不同位置
    计算机,我应该能够在那个保存的文件上写日志。

我已经完成了第一部分,但是当用户已经关闭对话框时,我不知道如何检索具有完整路径的文件名。

第 1 部分的我的代码如下。

void some_class ::on_write_file() 
    {

     // some code  ..
     bla bla bla
    switch( set_file_name_for_logging( QString::fromStdString( filename ) , this ) )
    {
      case QDialog::Accepted :
       std::cout <<" Retrive filename and full path name from the location where user has saved the file " and write on it;
      break;

     case QDialog::Rejected :
     break;
      default :
       throw_error( "Unexpected return value from save_ dump file  dialog" );
      break;
    }
  }

}

 int set_file_name_for_logging( const QString& str, som_class *cal ) 
   {
      QFileDialog file_dialog( cal );
      file_dialog.setDirectory(".");
      file_dialog.setAcceptMode(QFileDialog::AcceptSave);
      file_dialog.setNameFilter(  ("Text files (*.txt )") );
      file_dialog.selectFile( str );
      int ret = file_dialog.exec();
      return ret ;
 }

最佳答案

您可以使用 file_dialog.selectedFiles() 访问所选文件。另请查看静态函数 QFileDialog::getSaveFileName

关于c++ - 如何检索具有完整路径的文件名以将数据写入其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20677427/

相关文章:

c++ - 隐藏 QListView 项目的复选框

Qt QFile/QTemporaryFile无法读写

c++ - Qt信号槽重复代码避免

c++ - Dll 注入(inject)仅在未从 Visual Studio 启动时有效

c++ - 如果 extern "C"包含与 Qt 库冲突怎么办?

multithreading - QT多线程QImage改变

c++ - 如何在 QT3 中的 QFileDialog 窗口中添加复选框?

c++ - 防止 QTextEdit 小部件在有选择时滚动

c++ - QDataStream 和 QByteArray 的区别

c++ - QFile打开文件写入失败