Results 1 to 10 of 10

Thread: QFileDialog::getFileName how to set default extension?

  1. #1
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default QFileDialog::getFileName how to set default extension?

    I have file.txt. I type "file" in my filedialog, and I get error - "file is not found" i. How to add to "file" default extension? For example, .txt.

    my filedialog is:
    QFileDialog::getOpenFileName(this, "title", "text", "Text Files(*.txt));
    Last edited by somename; 26th May 2010 at 15:46.

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFileDialog::getFileName how to set default extension?

    Checkout the QFileDialog::setDefaultSuffix() method
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  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::getFileName how to set default extension?

    Or something like this:
    Qt Code:
    1. QFileDialog::getOpenFileName(this, tr("Open TXT File"),
    2. "",
    3. tr("Plain text file (*.txt)"));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QFileDialog::getFileName how to set default extension?

    I try this:

    1. QFileDialog *fileDialog = new QFileDialog;
    2. fileDialog->setDefaultSuffix("txt");
    3. QString some = fileDialog->getOpenFileName(this, "title", "text", "Text Files(*.txt));

    but I get same message : "file not found" when I type "file" without extension .txt

  5. #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::getFileName how to set default extension?

    I get now what You want to do.
    So You need to do an extra work to get this to work the way you want it to.
    First:

    Qt Code:
    1. QFileDialog *fileDialog = new QFileDialog;
    2. fileDialog->setFileMode( QFileDialog::AnyFile );
    3.  
    4. fileDialog->open( this, SLOT(slotSel(QString)) );
    To copy to clipboard, switch view to plain text mode 
    then declare slot i.e. slotSel(QString). This will bring a QT FileDialog, not the SYSTEM!.
    When You type i.e. 1 full path will be returned in slot
    i.e.:
    Qt Code:
    1. void MainWindow::slotSel( QString fileNameAndPath)
    2. {
    3. qDebug() << fileNameAndPath;
    4. }
    To copy to clipboard, switch view to plain text mode 
    And here You can do whatever You want with the string path.

  6. #6
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QFileDialog::getFileName how to set default extension?

    ok, but why setDefaultSuffix does not working?

  7. #7
    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::getFileName how to set default extension?

    Probably because of getOpenFileName. To be honest I don't know why setting fileMode to Any don't allow that. Maybe someone will know how to do that "easy way".

  8. #8
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QFileDialog::getFileName how to set default extension?

    The old Qt3 file dialog let the user type in the filter combo box. The Qt4 dialog made the combo box non-editable, and also did away with the ability to preview files. Both represent a giant step backward.

    I contacted Qt about this, and their "solution" is to rummage around in the QFileDialog class and change the combo box to editable, a procedure that is less than ideal but which works. Their solution looks like this:

    Qt Code:
    1. QComboBox* cb = this->findChild<QComboBox*>("fileTypeCombo");
    2. if (cb)
    3. cb->setEditable(true);
    To copy to clipboard, switch view to plain text mode 

    where 'this' is the QFileDialog; the code shown is executed in the constructor of a file dialog derived from QFileDialog.

    Obviously, if the trolls ever change the name of the combo box, this method will fail. But they assure me this is unlikely. They also said that my request to have both the preview and the ability to allow users to add their own filters at runtime will be brought to the attention of the maintainers, but there were no promises that we would see these features return.

  9. #9
    Join Date
    May 2010
    Posts
    39
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: QFileDialog::getFileName how to set default extension?

    My English is too bad.. Maybe you don't understand me?
    Now, I have question about this..

    QFileDialog *fileDialog = new QFileDialog;
    fileDialog->setFileMode( QFileDialog::AnyFile );
    fileDialog->open( this, SLOT(slotSel(QString)) );
    void MainWindow::slotSel( QString fileNameAndPath)
    {
    qDebug() << fileNameAndPath;
    }

    it works, but it don't use native dialogs!!!
    Is there some way to use native dialogs and set default extension?

  10. #10
    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::getFileName how to set default extension?

    The "trick" here is to use Qt File Dialog, because native implementation don't allow You to do what You want. And the "actual problem" is with disabling file check.
    Of course nothing stops You from using WinAPI, but the code will not be portable. Look for example here: http://msdn.microsoft.com/en-us/library/ms646927 .

Similar Threads

  1. QFileDialog and file extension
    By calhal in forum Qt Programming
    Replies: 3
    Last Post: 9th March 2010, 12:54
  2. How set default file to save in QFileDialog
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2009, 12:09
  3. why QFileDialog does not display the default winxp dialog?
    By mismael85 in forum Qt Programming
    Replies: 5
    Last Post: 21st March 2008, 13:39
  4. Using Qt with kernel extension dll
    By ramazangirgin in forum Qt Programming
    Replies: 1
    Last Post: 21st January 2008, 13:23
  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.