Important: Please read the Qt Code of Conduct -

A couple of problems with QFileDialog

  • Hello, I am trying to use QFileDialog in two different ways, but each has an annoying problem. Using Windows 7, Visual Studio 2015 with an MFC/Qt hybrid app, Qt 5.8.

    1. When I pass option DontUseNativeDialog to getOpenFilename, the File Open dialog shows in the file list only the file with the name passed in as third parameter to getOpenFilename. I want to see all files, just want to initialize the File name: text box to a pre-defined file name so it can be quickly selected.
    QString strFilter = "Media Files (*.ts;*.mpg);;All Files (*.*)";
    QString filePathName = "c:\\temp\\abc.ts";
    QFileDialog fileDlg;
    QString OpenedFileName = fileDlg.getOpenFilename(Q_NULLPTR, "Open", filePathName, strFilter, Q_NULLPTR, QFileDialog::DontUseNativeDialog);
    1. When I do not pass any options, so it's using the native (Windows) file dialog, the File Open dialog does not have problem #1, but shows the file name (passed in as third parameter) in the File name: text box cut off with the front part of it missing. There is plenty of room to show the whole file name but only the last 20 characters or so are shown, but depends on its length. Code is same as above, except below:
    QString OpenedFileName = fileDlg.getOpenFilename(Q_NULLPTR, "Open", filePathName, strFilter);

    Any suggestions?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    One thing that is done wrong is the filePathName content. Using backslashes like that you are escaping t and a. Since you are using Qt, use only forward slashes paths or use double slashes on Windows.

  • @SGaist Thanks. That was just for the purposes of this post. I am not really setting the filename explicitly. I have a proper filename that is opening a file properly, just not displaying like I want.

  • In addition, sometimes when I call fileDlg.getOpenFileName(...) it intermittently crashes on that line, access violation reading location 0xffffffffffffffff. Sometimes the 2nd time, sometimes the 10th time, never on the 1st time.

  • @Psychotron

    I want to see all files

    I think you need to set selectedFilter to something like the *.* (instead of Q_NULLPTR) from your strFilter, assuming that is not selected initially it will only currently be showing you *.ts;*.mpg initially?

    QFileDialog::DontUseNativeDialog: Docs claim:

    On Windows, and macOS, this static function will use the native file dialog and not a QFileDialog.

    Never mind behaviour, are you seeing any visual difference in overall dialog between your existing two calls?

    shows the file name (passed in as third parameter) in the File name: text box cut off with the front part of it missing.

    Wouldn't surprise me if the directory path is removed leaving only the filename at the right? Make sure your separators passed in are /s rather than \s.

    when I call fileDlg.getOpenFileName(...) it intermittently crashes

    It's a static method. You should be calling QFileDialog::getOpenFileName(...) ? Don't know what your fileDlg is, but if it's some kind of instance you've created that could be your prob here.... Ah, you are creating an instance (QFileDialog fileDlg;), that's perhaps the cause of all your probs here?

  • I believe my main problem here in #1 was in the filter. Should be a space between *.ts and *.mpg not a semicolon. Due to my team's preference, I am not passing the DontUseNativeDialog option now, so using the Windows native dialogs, and am having a problem with both the File Open and File Save dialogs crashing intermittently. I don't think we saw the crash with the DontUseNativeDialog option. Here is my code for both Open and Save. The open and save crash maybe every 5-10 times they are called in Debug. Don't really have any data on Release. Any ideas?

    //File open dialog
    QString strFilter = "Media Files (*.ts *.mpg);;All Files (*.*)";
    QString filePathName = <valid full path to filename with \\ as delimiters>
    QString openedFileName = QFileDialog::getOpenFileName( Q_NULLPTR, "Open", filePathName, strFilter );
    //File save dialog
    QString filePathName = <valid full path to filename with \\ as delimiters>
    QString savedFileName = QFileDialog::getSaveFileName( Q_NULLPTR, "Save As", filePathName, "TS (*.ts)" );

    Any reason I should replace the \ with / in the filenames before calling the static functions?

  • Lifetime Qt Champion

    Can you post a stack trace of your crash ?

    As for the / vs \: coherence and simplicity. Using / everywhere avoids you to have to write ifdefs when you prepare a path for *nix system and for Windows.

  • @SGaist

    void DisplayDlg::on_pushButtonLoad_clicked()
    	QString fileName = QFileDialog::getOpenFileName(Q_NULLPTR, tr("Open Overlay"), "", "XML (*.xml)");  //This line crashes sometimes.  Haven't seen it crash if I add option DontUseNativeDialog.
    mfc140d.dll!000007fec6f97e0c()	Unknown
    mfc140d.dll!000007fec6f5342c()	Unknown
    mfc140d.dll!000007fec6f52832()	Unknown
    user32.dll!0000000076ed9bdd()	Unknown
    user32.dll!0000000076ee6189()	Unknown
    user32.dll!0000000076ee4ea7()	Unknown
    user32.dll!0000000076ee4f36()	Unknown
    user32.dll!0000000076ee4f6c()	Unknown
    comdlg32.dll!000007feff1628e8()	Unknown
    qwindowsd.dll!000007fec494e5d1()	Unknown
    qwindowsd.dll!000007fec495b823()	Unknown
    qwindowsd.dll!000007fec495b7a5()	Unknown
    Qt5Widgetsd.dll!00000000650ca1d2()	Unknown
    Qt5Widgetsd.dll!0000000065048a8e()	Unknown
    Qt5Widgetsd.dll!0000000065048920()	Unknown
    MYDLL.dll!DisplayDlg::on_pushButtonLoad_clicked() Line 683	C++
    MYDLL.dll!DisplayDlg::qt_static_metacall(QObject * _o, QMetaObject::Call _c, int _id, void * * _a) Line 128	C++
    MYDLL.dll!DisplayDlg::qt_metacall(QMetaObject::Call _c, int _id, void * * _a) Line 184	C++
    Qt5Cored.dll!000000006715df44()	Unknown
    Qt5Cored.dll!00000000671a5e04()	Unknown
    Qt5Cored.dll!00000000671a5488()	Unknown
    Qt5Widgetsd.dll!0000000064fe877d()	Unknown
    Qt5Widgetsd.dll!0000000064fea8b4()	Unknown
    Qt5Widgetsd.dll!0000000064fe9977()	Unknown
    Qt5Widgetsd.dll!0000000064fe8f37()	Unknown
    Qt5Widgetsd.dll!0000000064cd695d()	Unknown
    Qt5Widgetsd.dll!0000000064fe89cb()	Unknown
    Qt5Widgetsd.dll!0000000065006011()	Unknown
    Qt5Widgetsd.dll!0000000064c76f3e()	Unknown
    Qt5Widgetsd.dll!0000000064c7217c()	Unknown
    Qt5Cored.dll!000000006714f666()	Unknown
    Qt5Cored.dll!00000000672b5cbb()	Unknown
    Qt5Widgetsd.dll!0000000064c79633()	Unknown
    Qt5Widgetsd.dll!0000000064d20e37()	Unknown
    Qt5Widgetsd.dll!0000000064d1f60a()	Unknown
    Qt5Widgetsd.dll!0000000064c76f3e()	Unknown
    Qt5Widgetsd.dll!0000000064c71973()	Unknown
    Qt5Cored.dll!000000006714f666()	Unknown
    Qt5Cored.dll!00000000672b5cbb()	Unknown
    Qt5Guid.dll!000007fedd97c19a()	Unknown
    Qt5Guid.dll!000007fedd97ed13()	Unknown
    Qt5Guid.dll!000007fedd939a45()	Unknown
    qwindowsd.dll!000007fec499fe82()	Unknown
    Qt5Cored.dll!000000006720ad0d()	Unknown
    [External Code]	
    MYAPP.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 26	C++
    [External Code]

Log in to reply