Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, February 2007
QFileDialog woes....


Message 1 in thread

(Qt 4.2.2 on Windows XP)

This should be really easy, but it's burning up an inordinate amount of 
time......

I'm trying to use QFileDialog to allow the user to save to a specified 
filename.

QFileDialog::getSaveFileName  does not look like it will fit the bill.  
I have ~5-10 filetypes the user can save to and thus I want to have 
multiple file filters.  getSaveFileName only allows one filter string.

So, I am trying the following:

        QFileDialog fd(this, "Save File", 
g_pSettings->getCurrentWorkingDirectory());
        fd.setFileMode(QFileDialog::AnyFile);
        fd.setAcceptMode(QFileDialog::AcceptSave);
        fd.setFilters(g_pSettings->getFileSaveFilters());    // file 
save filters is a QStringList
        QString fileName;
        if (fd.exec())
        {
            QStringList fileNames = fd.selectedFiles();
            if (fileNames.size() == 0)
            {
                return false;
            }
         
            fileName = fileNames[0];
         }

Problem 1: The dialog only shows the first QString in the QStringList 
for the filters.  All the other filter QStrings are not visible in the 
dialog's dropdown.

Problem 2: The filename returned may not have the proper file suffix 
appended to it.

For example, if I type "blah" as the filename, I get back the complete 
path including "blah" but with no file suffix.  The standard behavior 
should be for the QFileDialog to append the proper file filter suffix 
(as the Windows native dialog does).

I can retrieve the filter with fd.selectedFilter().  However, what I get 
back is the full filter string. e.g. "Text files (*.txt)"; so some extra 
processing is required.

Anyway, this leads me to believe I'm simply doing the wrong thing.  This 
should be pretty easy and not require a lot of kafuffle.  I can 
certainly do all the extra stuff to get the correct file name (problem 
#2) if I have to, but I don't understand why the multiple file filters 
does not work.

By the way, fd.setFilters also does not work for using the QFileDialog 
to read files (only the first QString is seen in the dialog's dropdown).

Cheers,
Mark


-- 
 [ signature omitted ] 

Message 2 in thread

Mark Thompson wrote:
> (Qt 4.2.2 on Windows XP)
> 
> This should be really easy, but it's burning up an inordinate amount of 
> time......
> 
> I'm trying to use QFileDialog to allow the user to save to a specified 
> filename.
> 
> QFileDialog::getSaveFileName  does not look like it will fit the bill.  
> I have ~5-10 filetypes the user can save to and thus I want to have 
> multiple file filters.  getSaveFileName only allows one filter string.
> 
> So, I am trying the following:
> 
>        QFileDialog fd(this, "Save File", 
> g_pSettings->getCurrentWorkingDirectory());
>        fd.setFileMode(QFileDialog::AnyFile);
>        fd.setAcceptMode(QFileDialog::AcceptSave);
>        fd.setFilters(g_pSettings->getFileSaveFilters());    // file save 
> filters is a QStringList
>        QString fileName;
>        if (fd.exec())
>        {
>            QStringList fileNames = fd.selectedFiles();
>            if (fileNames.size() == 0)
>            {
>                return false;
>            }
>                    fileName = fileNames[0];
>         }
> 
> Problem 1: The dialog only shows the first QString in the QStringList 
> for the filters.  All the other filter QStrings are not visible in the 
> dialog's dropdown.

That would be a bug in Qt 4.2.2 then. Just for the sake of debugging: 
what does fd->filters() return?

   QString filter
   foreach (filter, fd.filters()) {
     qDebug() << "The filter is: " << filter;
   }

> 
> Problem 2: The filename returned may not have the proper file suffix 
> appended to it.

I can confirm this for at least Qt 3 ;)

> 
> For example, if I type "blah" as the filename, I get back the complete 
> path including "blah" but with no file suffix.  The standard behavior 
> should be for the QFileDialog to append the proper file filter suffix 
> (as the Windows native dialog does).

Maybe an extra switch enabling this behaviour in a future Qt version 
would indeed save a lot of common work, something like

   QFileDialog::setAutoSuffix (bool enable);

Uhhh... I just checked, and they indeed have something like this:

   QFileDialog::setDefaultSuffix (const QString &suffix);

Off course this only works for a single suffix, assuming you only have 
one load/save format.

And thinking for another 5 seconds about it why auto-completion could 
not work with the selected file filter (at least not with the current 
behaviour of file filters):

Assume you have the following:

   dialog.setFilter("All C++ files (*.cpp *.cc *.C *.cxx *.c++)");

Now which file extension should be taken for auto-completion? It's not 
unique, it could be one of .cpp, .cc, .C etc.

So there's no way with the current Qt file dialog behaviour to 
auto-complete the suffix, given the selected file filter!

By the way, how is this solved in the Windows file dialog when you say 
it does auto-complete the file name?

> I can retrieve the filter with fd.selectedFilter().  However, what I get 
> back is the full filter string. e.g. "Text files (*.txt)"; so some extra 
> processing is required.

Yes, something like

   QStringList filters = g_pSettings->getFileSaveFilters();
   if (fd.selectedFilter() == filters[0]) {
     if (selectedFileName.endsWith (".txt", Qt::CaseInsensitive) == false) {
       // auto-complete
       selectedFileName.append (".txt");
     }
   }
   else if (fd.selectedFilter() == filters[1]) {
     ...
   }
   ...

Pretty cumbersome. Off course you can put this code into one of those 
many "utility" functions (in the famous "utility" DLL ;) and call it 
wherever you need to access file names...

> By the way, fd.setFilters also does not work for using the QFileDialog 
> to read files (only the first QString is seen in the dialog's dropdown).

Did you send in a bug report to Trolltech? See 
http://doc.trolltech.com/4.1/bughowto.html how to do so (check for 
existing task tracker entries).


Cheers, Oliver

--
 [ signature omitted ] 

Message 3 in thread


Till Oliver Knoll wrote:
>>
>> Problem 1: The dialog only shows the first QString in the QStringList 
>> for the filters.  All the other filter QStrings are not visible in 
>> the dialog's dropdown.
>
> That would be a bug in Qt 4.2.2 then. Just for the sake of debugging: 
> what does fd->filters() return?
>
>   QString filter
>   foreach (filter, fd.filters()) {
>     qDebug() << "The filter is: " << filter;
>   }
>
This returns just the first filter.  All the remaining filters in the 
QStringList are not returned.
>
> Did you send in a bug report to Trolltech? See 
> http://doc.trolltech.com/4.1/bughowto.html how to do so (check for 
> existing task tracker entries).
>
>
> Cheers, Oliver 
Thanks Oliver, I'll take a look at the bug reporting and log this one if 
it is not already there.

************************************
Mark Thompson
Planaria Software LLC
http://www.arguslab.com
mark@xxxxxxxxxxxx
************************************


--
 [ signature omitted ] 

Message 4 in thread

> Problem 1: The dialog only shows the first QString in the QStringList
> for the filters.  All the other filter QStrings are not visible in the
> dialog's dropdown.
My guess it is a mistake in QT documentation. It realy should be
 QStringList filters;
 filters << "Image files (*.png *.xpm *.jpg)"         << "Text files
(*.txt)"         << "Any files (*)"; QFileDialog dialog(this);
dialog.setFilters(filters.join("\n")); dialog.exec();
> Problem 2: The filename returned may not have the proper file suffix
> appended to it.
It would have first suffix from the list of suffixes in the selected filter.
In the example above, if you choose "image files" you will have "png"
suffix, and for "Any files" - no suffix at all.


--
 [ signature omitted ] 

Message 5 in thread

Hi George,

Thanks for the suggestion.  However, it does not work.

filters.join(...) returns a QString.
dialog.setFilters requires a QStringList.
dialog.setFilter does take a QString, but now all the joined strings are 
included in one big multi-line single choice in the drop-down in the 
dialog box.

Did I do something wrong?

Mark

George Brink wrote:
>> Problem 1: The dialog only shows the first QString in the QStringList
>> for the filters.  All the other filter QStrings are not visible in the
>> dialog's dropdown.
>>     
> My guess it is a mistake in QT documentation. It realy should be
>  QStringList filters;
>  filters << "Image files (*.png *.xpm *.jpg)"         << "Text files
> (*.txt)"         << "Any files (*)"; QFileDialog dialog(this);
> dialog.setFilters(filters.join("\n")); dialog.exec();
>   
>> Problem 2: The filename returned may not have the proper file suffix
>> appended to it.
>>     
> It would have first suffix from the list of suffixes in the selected filter.
> In the example above, if you choose "image files" you will have "png"
> suffix, and for "Any files" - no suffix at all.
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>
>
>   

-- 
 [ signature omitted ] 

Message 6 in thread

Hi,

> Thanks for the suggestion.  However, it does not work.
> 
> filters.join(...) returns a QString.
> dialog.setFilters requires a QStringList.
> dialog.setFilter does take a QString, but now all the joined strings are 
> included in one big multi-line single choice in the drop-down in the 
> dialog box.
> 
> Did I do something wrong?

I've just tried the following compilable, minimal example on Linux:
	#include <QApplication>
	#include <QFileDialog>
	#include <QtDebug>
	int main(int argc, char *argv[]) {
	  QApplication app(argc, argv);
	  QStringList filters;
	  filters << "Image files (*.png *.xpm *.jpg)"
	          << "Text files (*.txt)"
	          << "Any files (*)";
	  QFileDialog dialog(0);
	  dialog.setFilters(filters);
	  foreach (QString filter, dialog.filters())
	    qDebug() << "The filter is: " << filter;
	}


The output is:
	The filter is:  "Image files (*.png *.xpm *.jpg)"
	The filter is:  "Text files (*.txt)"
	The filter is:  "Any files (*)"

Isn't this what you get? What do you see? What do you expect?

--
 [ signature omitted ] 

Message 7 in thread

> 
> I've just tried the following compilable, minimal example on Linux:
> 	#include <QApplication>
> 	#include <QFileDialog>
> 	#include <QtDebug>
> 	int main(int argc, char *argv[]) {
> 	  QApplication app(argc, argv);
> 	  QStringList filters;
> 	  filters << "Image files (*.png *.xpm *.jpg)"
> 	          << "Text files (*.txt)"
> 	          << "Any files (*)";
> 	  QFileDialog dialog(0);
> 	  dialog.setFilters(filters);
> 	  foreach (QString filter, dialog.filters())
> 	    qDebug() << "The filter is: " << filter;
> 	}
> 
> 
> The output is:
> 	The filter is:  "Image files (*.png *.xpm *.jpg)"
> 	The filter is:  "Text files (*.txt)"
> 	The filter is:  "Any files (*)"
> 
> Isn't this what you get? What do you see? What do you expect?
> 

The OP did say the issue was on windows.

As a note, for QFileDialog in specific, I have found it to be so
INCOMPATIBLE with normal windows common control behavior, that I have
been writing my own FileDialog wrapper since QT 3.2

The problems I have seen, the autosuffix does not work the same as
windows.  The remembering of where the filedialog was used, is
OVERWRITTEN by QFiledialog

And other minor issues... When I have contacted support about this,
their response, was, since Linux does not have this functionality,
QFileDialog has to be the lowest common denominator of the
functionality.  That rather then add this type of functionality to the
linux version to match the capibilities of Windows and OSX, they went
the other way.

My response was, I understand Huh?  I guess "code less create more" only
counts for linux, not the other 95% of desktop environments

Scott

--
 [ signature omitted ] 

Message 8 in thread

I suggest you to give your opinion about the next Qt qfiledialog here...
http://qtdeveloper.net/archives/2007/02/05/new-qt-file-dialog/
(/I have been working on a new file dialog for Qt recently and would 
like to get feedback./)



Scott Aron Bloom a écrit :

>>I've just tried the following compilable, minimal example on Linux:
>>	#include <QApplication>
>>	#include <QFileDialog>
>>	#include <QtDebug>
>>	int main(int argc, char *argv[]) {
>>	  QApplication app(argc, argv);
>>	  QStringList filters;
>>	  filters << "Image files (*.png *.xpm *.jpg)"
>>	          << "Text files (*.txt)"
>>	          << "Any files (*)";
>>	  QFileDialog dialog(0);
>>	  dialog.setFilters(filters);
>>	  foreach (QString filter, dialog.filters())
>>	    qDebug() << "The filter is: " << filter;
>>	}
>>
>>
>>The output is:
>>	The filter is:  "Image files (*.png *.xpm *.jpg)"
>>	The filter is:  "Text files (*.txt)"
>>	The filter is:  "Any files (*)"
>>
>>Isn't this what you get? What do you see? What do you expect?
>>
>>    
>>
>
>The OP did say the issue was on windows.
>
>As a note, for QFileDialog in specific, I have found it to be so
>INCOMPATIBLE with normal windows common control behavior, that I have
>been writing my own FileDialog wrapper since QT 3.2
>
>The problems I have seen, the autosuffix does not work the same as
>windows.  The remembering of where the filedialog was used, is
>OVERWRITTEN by QFiledialog
>
>And other minor issues... When I have contacted support about this,
>their response, was, since Linux does not have this functionality,
>QFileDialog has to be the lowest common denominator of the
>functionality.  That rather then add this type of functionality to the
>linux version to match the capibilities of Windows and OSX, they went
>the other way.
>
>My response was, I understand Huh?  I guess "code less create more" only
>counts for linux, not the other 95% of desktop environments
>
>Scott
>
>--
>To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
>List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>  
>


Message 9 in thread

I don't want a custom dialog... All I want is full access to native options for FileDialog.

 

Scott

 

________________________________

From: veronique.lefrere@xxxxxx [mailto:veronique.lefrere@xxxxxx] 
Sent: Thursday, February 22, 2007 7:37 AM
To: Scott Aron Bloom
Cc: qt-interest@xxxxxxxxxxxxx
Subject: Re: QFileDialog woes....

 

I suggest you to give your opinion about the next Qt qfiledialog here...
http://qtdeveloper.net/archives/2007/02/05/new-qt-file-dialog/
(I have been working on a new file dialog for Qt recently and would like to get feedback.)



Scott Aron Bloom a écrit : 

	I've just tried the following compilable, minimal example on Linux:
	 #include <QApplication>
	 #include <QFileDialog>
	 #include <QtDebug>
	 int main(int argc, char *argv[]) {
	   QApplication app(argc, argv);
	   QStringList filters;
	   filters << "Image files (*.png *.xpm *.jpg)"
	           << "Text files (*.txt)"
	           << "Any files (*)";
	   QFileDialog dialog(0);
	   dialog.setFilters(filters);
	   foreach (QString filter, dialog.filters())
	     qDebug() << "The filter is: " << filter;
	 }
	 
	 
	The output is:
	 The filter is:  "Image files (*.png *.xpm *.jpg)"
	 The filter is:  "Text files (*.txt)"
	 The filter is:  "Any files (*)"
	 
	Isn't this what you get? What do you see? What do you expect?
	 
	    

 
The OP did say the issue was on windows.
 
As a note, for QFileDialog in specific, I have found it to be so
INCOMPATIBLE with normal windows common control behavior, that I have
been writing my own FileDialog wrapper since QT 3.2
 
The problems I have seen, the autosuffix does not work the same as
windows.  The remembering of where the filedialog was used, is
OVERWRITTEN by QFiledialog
 
And other minor issues... When I have contacted support about this,
their response, was, since Linux does not have this functionality,
QFileDialog has to be the lowest common denominator of the
functionality.  That rather then add this type of functionality to the
linux version to match the capibilities of Windows and OSX, they went
the other way.
 
My response was, I understand Huh?  I guess "code less create more" only
counts for linux, not the other 95% of desktop environments
 
Scott
 
--
 [ signature omitted ] 

Message 10 in thread

Hi,

> The OP did say the issue was on windows.

Indeed, but the OP didn't say it does not occur on Linux.

Hopefully someone can test on Windows.

> As a note, for QFileDialog in specific, I have found it to be so
> INCOMPATIBLE with normal windows common control behavior, that I have
> been writing my own FileDialog wrapper since QT 3.2
> 
> The problems I have seen, the autosuffix does not work the same as
> windows.  The remembering of where the filedialog was used, is
> OVERWRITTEN by QFiledialog

Which file dialog are we talking about? The native one (when using 
static QFileDialog functions) or the Qt one (when instantiating a 
QFileDialog)?

The latter I guess. Then the current path it is not really overwritten, 
it is simply not remembered - but I agree the result is identical and 
possibly annoying for application programmers. That said, it's very easy 
to add the functionality by yourself, as efficiently as if it were coded 
into Qt. About the "autosuffix" thing I don't know.

> And other minor issues... When I have contacted support about this,
> their response, was, since Linux does not have this functionality,
> QFileDialog has to be the lowest common denominator of the
> functionality.  That rather then add this type of functionality to the
> linux version to match the capibilities of Windows and OSX, they went
> the other way.
> 
> My response was, I understand Huh?  I guess "code less create more" only
> counts for linux, not the other 95% of desktop environments

You could have insisted. I tend to agree with you about remembering the 
path, but then I may be missing some vital information.

--
 [ signature omitted ] 

Message 11 in thread

> I've just tried the following compilable, minimal example on Linux:
> 	#include <QApplication>
> 	#include <QFileDialog>
> 	#include <QtDebug>
> 	int main(int argc, char *argv[]) {
> 	  QApplication app(argc, argv);
> 	  QStringList filters;
> 	  filters << "Image files (*.png *.xpm *.jpg)"
> 	          << "Text files (*.txt)"
> 	          << "Any files (*)";
> 	  QFileDialog dialog(0);
> 	  dialog.setFilters(filters);
> 	  foreach (QString filter, dialog.filters())
> 	    qDebug() << "The filter is: " << filter;
> 	}
> 
> 
> The output is:
> 	The filter is:  "Image files (*.png *.xpm *.jpg)"
> 	The filter is:  "Text files (*.txt)"
> 	The filter is:  "Any files (*)"
> 
> Isn't this what you get? What do you see? What do you expect?
> 
> --
> Dimitri

This example worked as expected on windows...

Except when I added the following line adter the foreach loop.

dialog.exec();

The dialog I got was a QT File Dialog, not the native one... 

However, I have never been happy or successful using QFileDialog outside
its static methods.

Scott


--
 [ signature omitted ] 

Message 12 in thread

Hi,

> This example worked as expected on windows...

Good.

> Except when I added the following line adter the foreach loop.
> 
> dialog.exec();
> 
> The dialog I got was a QT File Dialog, not the native one... 

This is expected.

--
 [ signature omitted ] 

Message 13 in thread

Mark,

You may want to try the static methods on QFileDialog, in this case, try
QFileDialog::getSaveFileName(....)

I have been using it for some time, it uses the native dialogs on
Windows and OSX, and filter issue definitely works.

As to the suffix issue, did you set the "setDefaultSuffix" ?  You may
want to try that.

Scott



> -----Original Message-----
> From: Mark Thompson [mailto:mark@xxxxxxxxxxxx]
> Sent: Wednesday, February 07, 2007 8:55 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: QFileDialog woes....
> 
> (Qt 4.2.2 on Windows XP)
> 
> This should be really easy, but it's burning up an inordinate amount
of
> time......
> 
> I'm trying to use QFileDialog to allow the user to save to a specified
> filename.
> 
> QFileDialog::getSaveFileName  does not look like it will fit the bill.
> I have ~5-10 filetypes the user can save to and thus I want to have
> multiple file filters.  getSaveFileName only allows one filter string.
> 
> So, I am trying the following:
> 
>         QFileDialog fd(this, "Save File",
> g_pSettings->getCurrentWorkingDirectory());
>         fd.setFileMode(QFileDialog::AnyFile);
>         fd.setAcceptMode(QFileDialog::AcceptSave);
>         fd.setFilters(g_pSettings->getFileSaveFilters());    // file
> save filters is a QStringList
>         QString fileName;
>         if (fd.exec())
>         {
>             QStringList fileNames = fd.selectedFiles();
>             if (fileNames.size() == 0)
>             {
>                 return false;
>             }
> 
>             fileName = fileNames[0];
>          }
> 
> Problem 1: The dialog only shows the first QString in the QStringList
> for the filters.  All the other filter QStrings are not visible in the
> dialog's dropdown.
> 
> Problem 2: The filename returned may not have the proper file suffix
> appended to it.
> 
> For example, if I type "blah" as the filename, I get back the complete
> path including "blah" but with no file suffix.  The standard behavior
> should be for the QFileDialog to append the proper file filter suffix
> (as the Windows native dialog does).
> 
> I can retrieve the filter with fd.selectedFilter().  However, what I
get
> back is the full filter string. e.g. "Text files (*.txt)"; so some
extra
> processing is required.
> 
> Anyway, this leads me to believe I'm simply doing the wrong thing.
This
> should be pretty easy and not require a lot of kafuffle.  I can
> certainly do all the extra stuff to get the correct file name (problem
> #2) if I have to, but I don't understand why the multiple file filters
> does not work.
> 
> By the way, fd.setFilters also does not work for using the QFileDialog
> to read files (only the first QString is seen in the dialog's
dropdown).
> 
> Cheers,
> Mark
> 
> 
> --
> 
> 
> 
> ************************************
> Mark Thompson
> Planaria Software LLC
> http://www.arguslab.com
> mark@xxxxxxxxxxxx
> ************************************
> 
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ]