Pergunta

Brief description: IN my Qt utility , I want that as soon as user hit the close button following things happens

  • 1) A File Dialog box appear with save /cancel options and with default file name in it.

  • 2) If the user has saved the file in different location on his
    computer , I should be able to write logs on that saved file.

I have done the first part but I am clueless on how to retrieve the file name with full path when the user has already closed the dialog box.

MY code for part 1 is given below.

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 ;
 }
Foi útil?

Solução 2

As written in my comment, you can use the following method:

QStringList QFileDialog::selectedFiles() const

Returns a list of strings containing the absolute paths of the selected files in the dialog. If no files are selected, or the mode is not ExistingFiles or ExistingFile, selectedFiles() contains the current path in the viewport.

Also, note that your code will need some refactoring to actually pass either the QFileDialog over or the path string itself. Currently, there is no direct access to them.

If you chooes to pass the QFileDialog somehow, you can get the string list, and it will only contain one item in your case, so you could use the first() convenience method then.

Outras dicas

You can access the chosen file using file_dialog.selectedFiles(). Also take a look at static function QFileDialog::getSaveFileName.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top