Re: Issue with QFileDialog
you might want to initialize your dialog? dialog is a null pointer! and by the way your connect statement doesn't make any sense.
Re: Issue with QFileDialog
Quote:
Originally Posted by
baluk
Code:
QString filepath
= dialog
->getOpenFileName
(this, tr
("Open File"),
"/home",tr
("Text files(*.txt)"));
QFileDialog::getOpenFileName() is a static function. You don't need an instance of QFileDialog for it, which is why this code is properly executed in the first place.
The following code is (arguably) better:
Code:
QString file = QFileDialog::getOpenFileName(this, tr
("Open File"),
"/home",tr
("Text files(*.txt)"));
Quote:
Code:
if(filepath.isNull())
connect(dialog,SIGNAL(rejected()),dialog,SLOT(close()));
As lykurg said, this doesn't make sense. That is for two reasons:
- QFileDialog::getOpenFileName() is a static function and blocking function. When the function returns, the file dialog is already closed.
- dialog is uninitialized
This basically means that your function should have been:
Code:
QString Directoryfinder
::getFilename() {
return QFileDialog::getOpenFileName(this, tr
("Open File"),
"/home", tr
("Text files (*.txt)");
}
When the dialog is canceled, the string will be null. When the dialog is accepted, the string will be non-null. Based on that you can make decisions. The signals and slots will be involved as soon as you stop using the static functions and start the open() paradigm.
Re: Issue with QFileDialog
oke now I have initialized the dialog. it works fine. but how can I close the dialog when the user press the "Cancel" button.
Re: Issue with QFileDialog
Have you tried the static version as franz had suggested? The dialog closes itself when pressing cancel.
Re: Issue with QFileDialog
Thanks to both of you for detailed explanation. It is working fine now except that I have to press "Cancel" twice before it is closed. It's not an issue but just want to know the reason.