Results 1 to 7 of 7

Thread: Changing a file extension inside the code

  1. #1
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Changing a file extension inside the code

    So currently a tool I developed has the ability to open two types of files - .xml & .tormatx(.tormatxis just a unique extension I'm using to enable users to not save them as .xml; just for consistency reasons)

    Qt Code:
    1. void Tormat_Main::open( const QString &p_fn ) {
    2. QString fileName;
    3. if( p_fn.isEmpty() ) { // ask user for a file
    4.  
    5. fileName = QFileDialog::getOpenFileName(this, tr("Open State"),
    6. baseFolder, tr("TomatFiles (*.tormatx*.xml)"));
    7.  
    8. if (fileName.isEmpty()) { return; }
    9. } else { // use passed file
    To copy to clipboard, switch view to plain text mode 

    Thats the code for opening a file...

    My question is, how do I make it so if a user opens up a .xml extension, the program will automatically convert it to .xsps without any user interaction????

    Thanks for the help guys

  2. #2
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing a file extension inside the code

    I guess I have a save function titled ...::Save

    How do I call it within the open function?

    Sorry I'm new to this qt/C++ stuff

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing a file extension inside the code

    Quote Originally Posted by fortyhideout12 View Post
    I guess I have a save function titled ...::Save
    You guess or you know? how to call:
    Qt Code:
    1. Save(/*your options here*/);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing a file extension inside the code

    yeah that was easy.

    Except It brings up a save dialogue right away now when I open up a file.

    I need the program to automatically change the extension...

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing a file extension inside the code

    Quote Originally Posted by fortyhideout12 View Post
    yeah that was easy.

    Except It brings up a save dialogue right away now when I open up a file.

    I need the program to automatically change the extension...
    Ehm, than change the code of the save method. Or create another function which saves a file without poping up a file dialog.

  6. #6
    Join Date
    Aug 2009
    Posts
    16
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Changing a file extension inside the code

    Qt Code:
    1. int Tormat_Main::save() {
    2. QString file_name = QDir::toNativeSeparators( savePath ).section( QDir::separator(), -1 );
    3. if( file_name.startsWith( "." ) ) { // the autosave starts with '.' so we want the user to save to a different location
    4. savePath.clear();
    5. }
    6.  
    7. if(savePath.isEmpty()) {
    8. savePath = QFileDialog::getSaveFileName(this, tr("Save File"), baseFolder, tr("TormatFiles (*.tormat)"));
    9. }
    10. if (savePath.isEmpty())
    11. return -1;
    12. try{
    13. QFileInfo file(savePath);
    14. writeXmlFile(file);
    15.  
    16. QString fn = QDir::toNativeSeparators( savePath ).section( QDir::separator(), -1 );
    17. Ui::MainWindow::info->setText( tr( "Saved state to '%1'" ).arg( fn ) );
    18. dialog->showOutput( tr( "Saved state to '%1'" ).arg( fn ) );
    19.  
    20. QString path_to_base = QDir::toNativeSeparators( savePath ).section( QDir::separator(), 0, -2 ); // to get path of state
    21. QString autosave_filename = QString( "%1\\.%2.autosave" ).arg( path_to_base, fn );
    22. if( autosave_filename != _autosaver->filename() ) { // user has saved to a new state, so update the autosaver
    23. _autosaver->deleteFile(); // delete old autosave file
    24. _autosaver->setFilename( autosave_filename );
    25. } else { // user has saved to same state name
    26. _autosaver->deleteFile(); // delete obsolete autosave file
    27. }
    28. _state_manager->addState( savePath );
    29. }catch(const std::exception &e) {
    30. Logger::LogMessage("Error Writing to File: " + QString(e.what()));
    31. QMessageBox::critical(this,"Save File Error",e.what());
    32. return -2;
    33. }
    34. return 0;
    35. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Changing a file extension inside the code

    And? What should I do with that code?!?

Similar Threads

  1. QFileDialog and file extension
    By calhal in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2010, 13:54
  2. Replies: 4
    Last Post: 16th February 2010, 18:42
  3. How to step inside(debug) webkit code?
    By nish in forum Qt Programming
    Replies: 5
    Last Post: 30th December 2009, 13:23
  4. Automatic file extension is not added to saved file on mac os x
    By sanjayshelke in forum Qt Programming
    Replies: 0
    Last Post: 30th October 2009, 07:56
  5. Can I use QRegExp to remove the extension of a file?
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2009, 14:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.