Search code examples
qtgui-testingqttest

Access QFiledialog programmatically


I am trying to do the system testing in QT created application. I have encountered the below problem. Open menu action in my application triggers a QFileDialog. I have handle(pointer) for the same. But i am not sure how to select the required file and trigger the open action.

Below things i have tried:

fileDial->setDirectory("xxxx");
fileDial->selectFile(xxx");
fileDial->fileSelected("xxxx");
fileDial->selectNameFilter("xxx");

and note that i haven't get any action for the

fileDial->findChildren<QAction*>(). 

Solution

  • QFileDialog is just a wrapper on a system dialog. That's why it's useless to search for any QActions there. Instead if you run you program in Windows you can use WIN API to deal with the dialog.

    Here is a simple example where some text is placed in the file name control and Open button is clicked:

    #define WAIT(A) while (!(A)) {}
    HWND dialogHandle, button, edit, combo, comboEx;
    WAIT(dialogHandle = FindWindow(NULL, L"Open"));
    WAIT(button = FindWindowEx(dialogHandle, NULL, L"Button", L"&Open"));
    WAIT(comboEx = FindWindowEx(dialogHandle, comboEx, L"ComboBoxEx32", NULL));
    WAIT(combo = FindWindowEx(comboEx, combo, L"ComboBox", NULL));
    WAIT(edit = FindWindowEx(combo, NULL, L"Edit", NULL));
    
    char text[] = "arc.h";
    SendMessageA(edit, WM_SETTEXT, 0, (LPARAM) text);
    
    SendMessage(button, BM_CLICK, 0, 0);