Results 1 to 11 of 11

Thread: QFileDialog set default name

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QFileDialog set default name

    Hello,
    How to set default file name for Qt QFileDialog (not the native one)?
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: QFileDialog set default name

    I don't know, but typing qfiledialog default filename to Google returns this:
    http://www.qtcentre.org/threads/1840...in-QFileDialog

    ...which gives you something to try.

  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QFileDialog set default name

    This is for the native dialogs, i.e. static function of QFileDialog like getSaveFileName etc. and when path PLUS file name is passed as the argument for start location/path then that file name is automatically filed with dialog file name. (and I have that working fine).

    What I ask is how to set default QFileDialog (i.e. Qt dialog) file name. In src I couldn't find any way to access that line edit for file name.

    EDIT:
    ------------------------
    Because copy is disabled in QFileDialog I can't access the d pointer to set Ui line edit text for file name.
    Modifying src is trouble some, because that force to redistribute modified Qt libs, also (I didn't checked it so maybe Im wrong) QFileDialog can be customized to disable this lineEdit so...

    What I did, to set custom file name in QFileDialog (Qt one not the native one) is this:

    Qt Code:
    1. class myQFileDialog : public QFileDialog
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit myQFileDialog(QWidget *parent = 0);
    6.  
    7. void initLineEdit(const QString initFileName) {
    8. QList<QLineEdit*> lineEdits = this->findChildren<QLineEdit*>();
    9.  
    10. for (int i = 0; i < lineEdits.count(); ++i)
    11. if (QString::compare(lineEdits.at(i)->objectName(), "fileNameEdit", Qt::CaseInsensitive) == 0) {
    12. lineEdits.at(i)->setText(initFileName);
    13. return;
    14. }
    15. }
    16.  
    17. signals:
    18.  
    19. public slots:
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    Note:
    If trolls decide to change name of the line edit from fileNameEdit to anything else then this function will simply dont work the way it suppose to.
    Last edited by Talei; 15th June 2012 at 13:15.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QFileDialog set default name

    QFileDialog will always use a native dialog if it can: no different to the static functions. QFileDialog will give you a non-native dialog if you are using slots in a sub-class or if you tell it explicitly not to use a native one.

    Anyway, this seems to answer your original question:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFileDialog dialog;
    9. dialog.setOptions(QFileDialog::DontUseNativeDialog); // with or without this
    10. dialog.setFileMode(QFileDialog::AnyFile);
    11. dialog.setAcceptMode(QFileDialog::AcceptSave);
    12. dialog.setDirectory(".");
    13. dialog.selectFile("somefile.txt"); // magic happens here
    14. int ret = dialog.exec();
    15. if (ret == QDialog::Accepted)
    16. qDebug() << dialog.selectedFiles();
    17. else
    18. qDebug() << "Cancelled :(";
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    Talei (16th June 2012)

  6. #5
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QFileDialog set default name

    Thanks that's exactly what I was looking for.
    I know setOption() and I'm using it to get (not by mistake but on purpose) Qt dialogs.
    Doc about selectFile() didn't specify that this will populate lineedit so I skipped that function... eh.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  7. #6
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileDialog set default name

    hello everyone
    why getSaveFileName don't set default save filename like UUID? it's empty
    Qt Code:
    1. QString strSaveFile =QFileDialog::getSaveFileName(0, "save", "./44aa756f-6c62-443d-a606-b758fde7e851.txt", "All Files(*.*)", 0, 0);
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileDialog set default name

    why getSaveFileName don't set default save filename like UUID? it's empty
    Because you are using the method incorrectly. The third argument is the initial directory, not the default file name. There is no way to specify the default file name using the QFileDialog static methods; if you want that, you must use the mechanism described in Chris's original answer in this thread.

  9. #8
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileDialog set default name

    sorry, i can't show detail.

    Qt version: Qt 5.5.1 MinGW 32bit
    OS: win7 64bit

    in qt5.5.1 assistant,
    http://doc.qt.io/qt-4.8/
    QString QFileDialog::getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, Options options = 0)

    This is a convenience static function that will return a file name selected by the user. The file does not have to exist.

    It creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.

    The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected. Only files that match the filter are shown. The filter selected is set to selectedFilter. The parameters dir, selectedFilter, and filter may be empty strings. Multiple filters are separated with ';;'. For instance:
    The options argument holds various options about how to run the dialog, see the QFileDialog::Option enum for more information on the flags you can pass.

    The default filter can be chosen by setting selectedFilter to the desired value.

    The dialog's caption is set to caption. If caption is not specified, a default caption will be used.

    On Windows, and OS X, this static function will use the native file dialog and not a QFileDialog.

    On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar. On OS X, with its native file dialog, the filter argument is ignored.

    On Unix/X11, the normal behavior of the file dialog is to resolve and follow symlinks. For example, if /usr/tmp is a symlink to /var/tmp, the file dialog will change to /var/tmp after entering /usr/tmp. If options includes DontResolveSymlinks the file dialog will treat symlinks as regular directories.

    Warning: Do not delete parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QFileDialog constructors.


    so, i test for this
    Qt Code:
    1. QString fileName = QFileDialog::getSaveFileName(0, "Save File","./abc.txt","*.*", 0, 0);
    To copy to clipboard, switch view to plain text mode 
    the default name exists.
    but
    Qt Code:
    1. QString fileName = QFileDialog::getSaveFileName(0, "Save File","./D99882DA-893E-4054-9344-FBF29E774780.txt","*.*", 0, 0);
    To copy to clipboard, switch view to plain text mode 
    the default name is empty, same as all like UUID's fileName;
    so why ? thanks.
    Attached Images Attached Images

  10. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileDialog set default name

    the default name is empty, same as all like UUID's fileName; so why ? thanks.
    Sorry, I did not see the part in the docs about using a file name as part of the dir string. My mistake.

    One other thing that it wrong is the way your specify the filter string. The filter should be something like "All files (*.*)". Maybe the file dialog is unable to parse the "*.*" string you are giving it.

    Otherwise, I do not know why there is a problem with using a GUID as the default filename.

  11. The following user says thank you to d_stranz for this useful post:

    voidbroken (26th February 2016)

  12. #10
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileDialog set default name

    Last edited by voidbroken; 26th February 2016 at 10:09.

  13. #11
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QFileDialog set default name


Similar Threads

  1. Replies: 9
    Last Post: 26th May 2010, 22:16
  2. Replies: 1
    Last Post: 20th January 2010, 22:59
  3. How set default file to save in QFileDialog
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 12:09
  4. why QFileDialog does not display the default winxp dialog?
    By mismael85 in forum Qt Programming
    Replies: 5
    Last Post: 21st March 2008, 13:39
  5. QFileDialog::getSaveFileName default extension
    By elcuco in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2007, 20:13

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.