Originally Posted by
baluk
QString filepath
= dialog
->getOpenFileName
(this, tr
("Open File"),
"/home",tr
("Text files(*.txt)"));
QString filepath = dialog->getOpenFileName(this, tr("Open File"),"/home",tr("Text files(*.txt)"));
To copy to clipboard, switch view to plain text mode
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:
QString file = QFileDialog::getOpenFileName(this, tr
("Open File"),
"/home",tr
("Text files(*.txt)"));
QString file = QFileDialog::getOpenFileName(this, tr("Open File"),"/home",tr("Text files(*.txt)"));
To copy to clipboard, switch view to plain text mode
if(filepath.isNull())
connect(dialog,SIGNAL(rejected()),dialog,SLOT(close()));
if(filepath.isNull())
connect(dialog,SIGNAL(rejected()),dialog,SLOT(close()));
To copy to clipboard, switch view to plain text mode
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
return filepath;
return filepath;
To copy to clipboard, switch view to plain text mode
This basically means that your function should have been:
QString Directoryfinder
::getFilename() {
return QFileDialog::getOpenFileName(this, tr
("Open File"),
"/home", tr
("Text files (*.txt)");
}
QString Directoryfinder::getFilename()
{
return QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Text files (*.txt)");
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks