Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open-file dialog returning gibberish?

Open-file dialog returning gibberish?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 230 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Publicnamer
    wrote on last edited by Publicnamer
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @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();
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved