Open-file dialog returning gibberish?
-
wrote on 9 Oct 2021, 14:38 last edited by Publicnamer 10 Sept 2021, 14:39
I've tried using the open-file dialog to get a filename or file path and it consistently gives a gibberish value:
QString filename = QFileDialog::getOpenFileName(this, "Open text file", getenv("HOME"), "Text Files (*.txt)"); const char *path = filename.toStdString().c_str();
The result is always a string that is nothing like the filename that was selected.
What is the correct way to get a C string path or filename from QFileDialog? -
Lifetime Qt Championwrote on 9 Oct 2021, 15:20 last edited by Christian Ehrlicher 10 Sept 2021, 15:20
@Publicnamer said in Open-file dialog returning gibberish?:
The result is always a string that is nothing like the filename that was selected.
I would be surprised if you would return something valid... You create a temporary std::string which goes out-of-scope immediatelly.
Why do you need a const char* for this at all?QString filename = ... std::string filenameStd = filename.toStdString(); const char *path = filenameStd .c_str();
-
Hi,
@Publicnamer said in Open-file dialog returning gibberish?:
const char *path = filename.toStdString().c_str();
You are keeping the address of a temporary std::string that you convert to a c string.
So there's a high chance that path points to garbage memory.
1/3