简体   繁体   中英

QFileDialog and setDefaultSuffix for Mac Sandbox

I would like to set default file extension in QFileDialog for saving files in my app ( it needs to run in mac app store sandbox ). The default behaviour of setDefaultSuffix on mac ( without sandbox) is that it adds an extension to any name that user enters.

This works differently when sandbox is on; it just adds the extension string at the begining of interaction, but when user deletes the proposed file name ( with extension ) and enters one without extension, the file from QFileDialog does not include the suffix. Is there a way to hack the Dialog to behave the same way in sandbox?

I'm using Qt 4.8.5 ( no way to upgrade now )

I don't think you can change the behaviour of Mac sandbox but you can add simple workaround for Mac like that:

QString suffix = ".txt";  // your suffix here
QString fileName = ... // get filename after QFileDialog work
QFileInfo file(filename);
if(file.suffix().isEmpty()) fileName += suffix ;

It turns out that using static method to show QFileDialog enables correct default suffix to work in Mac sandbox, so instead of creating QFileDialog instance and setting default suffix just use:

getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), 
                const QString & dir = QString(), const QString & filter = QString(), 
                QString * selectedFilter = 0, Options options = 0)

with filter set to allow only extensions you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM