Important: Please read the Qt Code of Conduct -

QFileDialog only gets auto focus on first opening

  • My application contains a QFileDialog, opened by clicking a QPushButton, to open one or multiple files within the application. When the QFileDialog is opened for the very first time of the applications runtime the QFileDialog automatically gets the focus over the QMainWindow which is the wanted and expected behaviour. However if the QFileDialog is now closed and later opened again, through the exact same method, the QMainWindow keeps the focus and the user has to Alt+Tab or click the dialog to set the focus to it. Note that I open the dialog with the exec() method so it should behave as a modal dialog. I have tried messing with setFocus() and setFocusPolicy but nothing seems to affect the current behaviour. The QPushButton in question has its clicked signal connected to the following slot:

    void DataWindow::addData()
        QFileDialog* dialog = new QFileDialog(this);
        dialog->setNameFilter("All files (*);;");
        if (dialog->exec()) {
            // Handle selected files 
        delete dialog;

    How can I get the QFileDialog to get the focus everytime it gets opened?

    Edit: I am running Qt v5.2.1 on Ubuntu 14.04

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should add the version of Qt you are using as well as the OS you are running

Log in to reply