0

I have implemented the getOPenFileName in a handler in qt (in a when_pushbutton_clicked more specifically). How can I save the produced string in a QString in main rather than inside the handler?

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55

1 Answers1

0

You can use a signal connection to save in a QString variable the path of your file name.

const QString fileName = QFileDialog::getOpenFileName(0, tr("Select the file"), getLastDirectory(), "Txt Files (*.txt)");
if (fileName.isEmpty()) {
    // No file was selected
    return;
}
// then emit the signal
emit fileWasSelected(fileName);

In your main function you cant handle the event in the main class by a simple connection:

QObject::connect(yourClass, &YourClass::fileWasSelected, [&](const QString& filename) {
  // Now, do what you want with your path 
}):

Another way, its to save the file in a private variable, and set up a getter:

class MyClass {
   ....
public:
    inline QString path() const { return _path; }
private:
    QString _path;
}

And then access to the variable from the main.

mohabouje
  • 3,867
  • 2
  • 14
  • 28