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?
Code:
void snapShot::on_actionSave_as_activated()
{
if ( preview->palette().brush ( backgroundRole() ).texture().isNull() )
{
return;
}
fd.setWindowTitle ( tr ( "Save as..." ) );
filters << tr ( "PNG Images (*.png)" );
filters << tr ( "JPG Images (*.jpg *.jpeg)" );
filters << tr ( "XPM Images (*.xpm)" );
filters << tr ( "All Images (*.png *.jpg *.jpeg *.xpm)" );
fd.setNameFilters ( filters );
fd.setDirectory ( WorkDir );
if ( fd.
exec() ==QDialog::Accepted ) {
if ( !files.isEmpty() )
fName = files[0];
}
if ( fName.length() >0 )
{
if ( saveInFile ( fName ) ) {
fName.append(".png");
setWindowTitle
( QFileInfo(fName
).
absoluteFilePath() + " - " + initWindowTitle
);
}
}
}
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!
Re: QFileDialog for choosing filename to save data
Usually I use this,
Code:
tr("Books (*.book);;All Files (*)"));
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.
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:
Code:
Q_OBJECT
public:
MyFileDialog
(QWidget *parent
= 0);
virtual ~MyFileDialog();
private slots:
void appendExtension( const QString& filter);
};
connect(this, SIGNAL(filterSelected( const QString& )), this, SLOT(appendExtension( const QString& )));
}
MyFileDialog::~MyFileDialog()
{
}
void MyFileDialog::appendExtension( const QString& filter)
{
setDefaultSuffix("png");
if (filter.contains("xpm") && !filter.contains("png") ) setDefaultSuffix("xpm");
else if (filter.contains("jpg") && !filter.contains("png") ) setDefaultSuffix("jpg");
qDebug() << defaultSuffix();
}