views:

41

answers:

3

Hi guys .. Well I'm using the following code to get the filename for a file that needs to be stored ..

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"/home/user/MyDocs/",tr("JPG files (*.jpg);;BMP files (*.bmp);;PNG files (*.png)"));

I'm providing the user with a number of options regarding the file format in which the file is to be saved .. however, the returned QString only gives me the prefix filename the user chose, not the suffix .. so I dont know which file format the user chose .. How can I detect the file format the user chose ?

+1  A: 

This looks interesting for you:

http://www.qtcentre.org/threads/8503-QFileDialog-getSaveFileName-default-extension

It uses QFileInfo on the string that was entered in a QFileDialog.

Google is your friend. And please try to accept some answers for your questions.

rubenvb
Sure i'll accept an answer here once I get the answer I need .. Uh, this is not not working too .. QFileInfo will only work if the string passed into it (namely 's' in the example you linked to) contains a filename which has a suffix already .. for example, if the string was "file.ext", the suffix would return "ext" .. My problem is that the filename in the first place does NOT have a suffix in it .. That is, the fileName I get is coming like "/home/user/MyDocs/filename" ... no suffix ..
Ahmad
A: 

You need to use the 5th optional string
I usually do it like this:

#define JPEG_FILES "JPG files (*.jpg)"
#define BMP_FILES "BMP files (*.bmp)"
#define PNG_FILES "PNG files (*.png)"

QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
        "/home/user/MyDocs/",
        JPEG_FILES ";;" BMP_FILES ";;" PNG_FILES, &selecteFilter);

if (fileName.isNull())
  return;
if (selectedFilter == JPEG_FILES) {
  ...
} else if (selectedFilter == BMP_FILES) { 
  ...
} else if (selectedFilter == PNG_FILES) {
  ...
} else {  
    // something strange happened 
}

The compiler takes care to concatenate the literal strings in the argument.

I'm not sure how the returned string interacts with tr(). You'll have to test and find out. probably need to un-translate it.
It could have been nicer if the function would return the index of the selected filter but alas, it does not.

A nicer solution would be to put the filters in a list, create a string from it and then compare to the returned selected filter string to the ones in the list. This would also solve the tr() problem.

shoosh
I just tried this and it didn't work for me .. the returning selectedString is empty ..
Ahmad
@Ahmad: you say "returning selectedString" are you testing `selectedFilter` from the example here or `filename`?
Evan Teran
This is the code I was using:QString sf;QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"/home/user/MyDocs/",tr("JPG files (*.jpg);;BMP files (*.bmp);;PNG files (*.png)"),sf does not contain anything after the above two lines have been executed .. its empty ..
Ahmad
sf is going to have something only if you press "ok" and not cancel the dialog.
shoosh
As I read these docs (http://doc.qt.nokia.com/4.6/qfiledialog.html#getSaveFileName), it appears that selectedFilter is used to set the default filter shown when the dialog appears. It might be set to the filter the user selected on return from the function, but the docs don't clearly state that it will be.
Caleb Huitt - cjhuitt
A: 

The code in the question works in Windows (Qt 4.6.2 and Win XP). fileName contains the selected extension. But you are obviously using something else Windows, so you could try this workaround:

QFileDialog dialog(this, tr("Save as ..."), "/home/user/MyDocs/");
dialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList filters;
filters << "JPG files (*.jpg)" << "BMP files (*.bmp)" << "PNG files (*.png)";
dialog.setNameFilters(filters);
if (dialog.exec() == QDialog::Accepted)
{
    QString selectedFilter = dialog.selectedNameFilter();
    QString fileName = dialog.selectedFiles()[0];
}

That is a slighty modified code from here.

Roku
This doesn't do the same as the OPs code. it opens QTs file dialog instead of the platforms file dialog.
shoosh