6

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.

LOK CARD
  • 293
  • 2
  • 16
  • 1
    http://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 May 26 '15 at 03:13
  • 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 May 26 '15 at 03:23
  • Yep. check for !filenName.isNull() will do. – Alexander V May 26 '15 at 04:03

1 Answers1

11

thanks to AlexanderVX

the solution is simple :

if(!fileName.isEmpty()&& !fileName.isNull()){
// functionA
}
LOK CARD
  • 293
  • 2
  • 16