PDA

View Full Version : QFileDialog::getFileName how to set default extension?



somename
26th May 2010, 15:41
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));

calhal
26th May 2010, 15:47
Checkout the QFileDialog::setDefaultSuffix() method

Talei
26th May 2010, 15:49
Or something like this:

QFileDialog::getOpenFileName(this, tr("Open TXT File"),
"",
tr("Plain text file (*.txt)"));

somename
26th May 2010, 16:11
I try this:


QFileDialog *fileDialog = new QFileDialog;
fileDialog->setDefaultSuffix("txt");
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

Talei
26th May 2010, 17:14
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:


QFileDialog *fileDialog = new QFileDialog;
fileDialog->setFileMode( QFileDialog::AnyFile );

fileDialog->open( this, SLOT(slotSel(QString)) );
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.:

void MainWindow::slotSel( QString fileNameAndPath)
{
qDebug() << fileNameAndPath;
}

And here You can do whatever You want with the string path.

somename
26th May 2010, 17:18
ok, but why setDefaultSuffix does not working?

Talei
26th May 2010, 17:37
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".

SixDegrees
26th May 2010, 19:04
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:


QComboBox* cb = this->findChild<QComboBox*>("fileTypeCombo");
if (cb)
cb->setEditable(true);

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.

somename
26th May 2010, 19:18
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?

Talei
26th May 2010, 22:16
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 .