5
votes

I'm new to QT. Currently in my project I implemented QFileDialog.

In my usecase : whenever user choose a text file, it executes functionA. However, I found that if I click cancel in the fileDialog, functionA will still be executed.

This is my code snipplet:

QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "/home",
                                                 tr("Text File (*.txt"));

// I want something like following :

if(QFileDialog.isOkButtonClicked)
{
    // execute functionsA
}

I looked into QFileDialog documentation and nothing similiar. Is it possible to achieve this or is there any other solution ? thanks.

1
doc.qt.io/qt-5/qfiledialog.html#getOpenFileName The doc is saying: "This is a convenience static function that returns an existing file selected by the user. If the user presses Cancel, it returns a null string."Alexander V
Thanks for reply @AlexanderVX. I added if(!fileName.isEmpty()&&fileName!="") and it works. But i'm not sure is it what u connote ?LOK CARD
Yep. check for !filenName.isNull() will do.Alexander V

1 Answers

9
votes

thanks to AlexanderVX

the solution is simple :

if(!fileName.isEmpty()&& !fileName.isNull()){
// functionA
}