Results 1 to 6 of 6

Thread: Adding default extension to the file

  1. #1
    Join Date
    Oct 2015
    Posts
    35
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Adding default extension to the file

    Hi,

    I'm trying to save the files with a default extension. With the following code snippet, the file does save with the specified file name but it doesn't attach the extension to it. Can someone please help me. Thanks in advance.

    Qt Code:
    1. QString fileName = QFileDialog::getSaveFileName(this, tr("Save Logs"),"/Tmp/",tr("*.csv"));
    2. if(!fileName.isEmpty())
    3. {
    4. QFile f(fileName);
    5. if(!fileName.endsWith(".csv"))
    6. fileName = fileName + ".csv";
    7. f.open(QIODevice::WriteOnly);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Adding default extension to the file

    You are modifying fileName after you've used it to initialize "f".

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    35
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Adding default extension to the file

    Could you please elaborate. How do I use fileName instead of f and check if the user entered extension, if the extension is not entered attach the extension and save it.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,345
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Adding default extension to the file

    Qt Code:
    1. if ( !fileName.isEmpty() )
    2. {
    3. if ( !fileName.endsWith(".csv") )
    4. fileName += ".csv";
    5.  
    6. QFile f( fileName );
    7. f.open( QIODevice::WriteOnly);
    8.  
    9. // ...
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to d_stranz for this useful post:

    rookee (18th November 2015)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Adding default extension to the file

    Quote Originally Posted by rookee View Post
    Could you please elaborate.
    C++ is an imperative language.
    Each statement potentially changes the state of the machine.
    Statements are executed in order of sequence in the code.

    so
    Qt Code:
    1. QFile f(fileName);
    2. fileName = ...;
    To copy to clipboard, switch view to plain text mode 
    first executes the creation of "f", then the modification of fileName.

    Quote Originally Posted by rookee View Post
    How do I use fileName instead of f and check if the user entered extension
    You currently don't use f to check for the extension so that questions does not make any sense.

    Quote Originally Posted by rookee View Post
    if the extension is not entered attach the extension and save it.
    You do that already.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    rookee (18th November 2015)

  8. #6
    Join Date
    Oct 2015
    Posts
    35
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Adding default extension to the file

    Thanks a lot d_stranz and anda_skoa. I really appreciate it.

Similar Threads

  1. Adding library with .a extension
    By ftl_embedded in forum Qt Programming
    Replies: 9
    Last Post: 4th September 2015, 09:49
  2. Replies: 9
    Last Post: 26th May 2010, 23:16
  3. Replies: 4
    Last Post: 16th February 2010, 18:42
  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. QFileDialog::getSaveFileName default extension
    By elcuco in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2007, 21:13

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
  •  
Qt is a trademark of The Qt Company.