Results 1 to 3 of 3

Thread: QFileDialog for choosing filename to save data

  1. #1
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry QFileDialog for choosing filename to save data

    Hello!
    Would you please tell me how to append filter text to filename as extension?

    Found this solution:
    http://www.qtcentre.org/forum/f-qt-p...ht=QFileDialog

    but maybe there is a lighter one for such simple (I hope) operation?

    Qt Code:
    1. void snapShot::on_actionSave_as_activated()
    2. {
    3. if ( preview->palette().brush ( backgroundRole() ).texture().isNull() )
    4. {
    5. QMessageBox::critical ( this,tr ( "Error" ),tr ( "Nothing to save" ),QMessageBox::Ok,0 );
    6. return;
    7. }
    8.  
    9. QString fName;
    10. QString frmts;
    11. QFileDialog fd ( this );
    12. fd.setAcceptMode ( QFileDialog::AcceptSave );
    13. fd.setWindowTitle ( tr ( "Save as..." ) );
    14.  
    15. QStringList filters ( fd.nameFilters() );
    16. filters << tr ( "PNG Images (*.png)" );
    17. filters << tr ( "JPG Images (*.jpg *.jpeg)" );
    18. filters << tr ( "XPM Images (*.xpm)" );
    19. filters << tr ( "All Images (*.png *.jpg *.jpeg *.xpm)" );
    20. fd.setNameFilters ( filters );
    21.  
    22. fd.setDirectory ( WorkDir );
    23. fd.setFileMode ( QFileDialog::AnyFile );
    24. if ( fd.exec() ==QDialog::Accepted )
    25. {
    26. QStringList files = fd.selectedFiles();
    27. if ( !files.isEmpty() )
    28. fName = files[0];
    29. }
    30. if ( fName.length() >0 )
    31. {
    32. if ( saveInFile ( fName ) ) {
    33.  
    34. if( !QFileInfo(fName).exists() )
    35. fName.append(".png");
    36. setWindowTitle( QFileInfo(fName).absoluteFilePath() + " - " + initWindowTitle);
    37. }
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    When the user print "file.jpg" - it's ok, but when he print just "file" and then choose filter ("jpg") - exec() returns "file" without any information about desired extension...

    Thanks in advance!

  2. #2
    Join Date
    Mar 2009
    Posts
    25
    Thanked 2 Times in 2 Posts

    Default Re: QFileDialog for choosing filename to save data

    Usually I use this,

    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(this, tr("Open Book"),
    2. QDir::currentPath(),
    3. tr("Books (*.book);;All Files (*)"));
    To copy to clipboard, switch view to plain text mode 

    See your online documentation for the method.

    Filter - notice the name "filter" - only affects what you see on the file list.
    It has nothing to do with what is returned. This is generic and not specific to Qt.

  3. The following user says thank you to Hiker Hauk for this useful post:

    araglin (1st April 2009)

  4. #3
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog for choosing filename to save data

    Thanks for your answer.
    Actually I agree with you - filter must only filter, nothing more...
    but there was a demand from users, so I solved this issue with the following code, maybe it will be helpful for someone else:
    Qt Code:
    1. class MyFileDialog : public QFileDialog {
    2. Q_OBJECT
    3. public:
    4. MyFileDialog (QWidget *parent = 0);
    5. virtual ~MyFileDialog();
    6. private slots:
    7. void appendExtension( const QString& filter);
    8. };
    9.  
    10. MyFileDialog::MyFileDialog(QWidget *parent): QFileDialog(parent) {
    11. connect(this, SIGNAL(filterSelected( const QString& )), this, SLOT(appendExtension( const QString& )));
    12. }
    13. MyFileDialog::~MyFileDialog()
    14. {
    15. }
    16. void MyFileDialog::appendExtension( const QString& filter)
    17. {
    18. setDefaultSuffix("png");
    19. if (filter.contains("xpm") && !filter.contains("png") ) setDefaultSuffix("xpm");
    20. else if (filter.contains("jpg") && !filter.contains("png") ) setDefaultSuffix("jpg");
    21. qDebug() << defaultSuffix();
    22. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to save file with QFileDialog
    By pnikolov in forum Qt Programming
    Replies: 11
    Last Post: 1st June 2012, 10:23
  2. How set default file to save in QFileDialog
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 12:09
  3. QFileDialog filename list order
    By joelthelion in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 14:58
  4. Replies: 2
    Last Post: 8th August 2008, 01: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.