Results 1 to 3 of 3

Thread: QFileDialog::getSaveFileName default extension

  1. #1
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QFileDialog::getSaveFileName default extension

    I want to add a extension to the file name a user chooses. Lets assume the piece of code attached. Lets also assume that the user chooses to save as "html", and he chooses the filename "file1".

    I want to add to the filename, the "html" extension. However, on my code the variable sf contains the full string showed at the bottom 'HTML Files(*.html *.htm)' and I just want "html".

    I can handcraft something myself, like having a hash, and creating the string to be passed to the QFileDialog::getSaveFileName call, but I am hoping for a better tirck.


    Qt Code:
    1. static QString sf;
    2. QString s = QFileDialog::getSaveFileName(
    3. this,
    4. "Choose a file to save to",
    5. "",
    6. "Text files (*.txt *.utf8);; HTML Files(*.html *.htm)",
    7. &sf
    8. );
    9.  
    10. if (s.isEmpty())
    11. return;
    12.  
    13. bool status;
    14. QString s_lower = s.toLower();
    15.  
    16. // what if the users did not specify a suffix...?
    17. QFileInfo f( s );
    18. if (f.suffix().isEmpty())
    19. {
    20. //
    21. qDebug("no suffix, adding %s", qPrintable( sf ) );
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog::getSaveFileName default extension

    Go ahead and use a map, or something like that.

    Regards

  3. #3
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog::getSaveFileName default extension

    This is *so* lame... Trolltech needs to fix this issue, this is way wrong... anyway, this is what I am using right now (not very pleased with it, since the constructor now contains some text which should be part of the save function... which will make it hard to debug):

    Qt Code:
    1. // somewhere in the constructor
    2. extByMessage[ tr("Text files (*.txt *.utf8)") ] = ".txt";
    3. extByMessage[ tr("HTML Files (*.html *.htm)") ] = ".html";
    4.  
    5. QHashIterator<QString, QString> i(extByMessage);
    6. while (i.hasNext())
    7. {
    8. i.next();
    9. saveMessage += i.key();
    10. if (i.hasNext())
    11. saveMessage += ";;";
    12. }
    13.  
    14. // the save function
    15. QString s = QFileDialog::getSaveFileName(
    16. this,
    17. tr("Choose a file to save to"),
    18. lastDir,
    19. saveMessage,
    20. &sf
    21. );
    22.  
    23. if (s.isEmpty())
    24. return;
    25.  
    26. bool status;
    27.  
    28. // what if the users did not specify a suffix...?
    29. QFileInfo f( s );
    30. if (f.suffix().isEmpty())
    31. {
    32. // http://www.qtcentre.org/forum/f-qt-programming-2/t-qfiledialoggetsavefilename-default-extension-8503.html
    33. s += extByMessage[sf];
    34. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Default size of a QListWidget inside a QDockWidget
    By rakuco in forum Qt Programming
    Replies: 0
    Last Post: 25th July 2007, 08:01
  2. qmake default includes
    By Gopala Krishna in forum Installation and Deployment
    Replies: 5
    Last Post: 17th July 2007, 20:47
  3. default argument compiler warning
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2007, 14:57
  4. Default value for QFont
    By Kapil in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2006, 06:40
  5. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33

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.