Congratulations to our 2022 Qt Champions!

Center QFileDialog

  • If I call:

    @QFileDialog::getOpenFileName(this, tr("Open Project"));@

    were this is a QMainWindow, the file dialog is not centered. According to the Qt documentation the dialog should be centered if the "parent is not 0". I'm using Win7 (64bit) and Qt 4.7

    Does anyone know why this dialog is not centered? I've also tried to change the parent to another widget, but without any success.

  • Where does the dialog pop up? According to the docs it should be centered over the parent widget, not the whole screen.

  • it pops up at the top left corner of my parent widget, not the center of my parent widget

  • Could you provide a short sample with source code so that we can have a detail look at it?

  • well, the line provided above is all the source code. I only have a button, and this is the only like in the mouse-clicked event function.

  • mouse-clicked event function? Are you overriding one of the event handlers (mousePressEvent(), mouseReleaseEvent())? If so, of what type is the this pointer in your call? Or are you in a slot connected to the clicked signal of your button?

  • I'm not 100% sure what you are asking, I'm just overriding the mouse-clicked event of the button. I'm not sure of what pointer you are talking. And does this influence the QFileDialog events?

    @void XapoWindow::on_loadButton_clicked()
    QFileDialog::getOpenFileName(this->ui->centralWidget, tr("Open Xapo Project"));

  • From what you write, it seems that XapoWindow is of type "QMainWindow": In that case, you should use this (i.e. your main window) as the parent to getOpenFileName(), not the centralWidget.

    Just for your information: in XapoWindow::on_loadButton_clicked() you do not override a mouse event, in Qt world this is called a slot, that got automatically connected to the signal clicked of your button. See "Signals & Slots": and "The Event System": in the Qt docs for some further information and the differences between the two.

  • Thanks for that, and yes, this is of type QMainWindow. I use that as parent, but the dialog is still not centered

  • So it would be helpful if you could post the complete source code of your sample program or provide a download link. Otherwise we cannot look into the details or reproduce the faulty behaviour.

  • main.cpp:

    @#include <QtGui/QApplication>
    #include "XapoInterface/xapowindow.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv); //just using it as debug command line
    XapoWindow w;;
    return a.exec();


    @#ifndef XAPOWINDOW_H
    #define XAPOWINDOW_H

    #include <QMainWindow>
    #include <QFileDialog>

    namespace Ui {
    class XapoWindow;

    class XapoWindow : public QMainWindow



     A constructor for creating a window
    explicit XapoWindow(QWidget *parent = 0);
     A destructor for destructing the window


    Ui::XapoWindow *ui;

    private slots:

    void on_loadButton_clicked();


    #endif // XAPOWINDOW_H@


    @#include "XapoInterface/xapowindow.h"
    #include "ui_xapowindow.h"

    XapoWindow::XapoWindow(QWidget *parent) :
    ui(new Ui::XapoWindow)

    delete ui;

    void XapoWindow::on_loadButton_clicked()
    QFileDialog::getOpenFileName(this->ui->centralWidget, tr("Open Xapo Project"));

    The XapoWindow is supported by 'n UI-file, I don't think you want it (or do you?), it is just a window with a button on it.

  • Without the .ui file and the .pro file we cannot compile the program.

  • Sorry for the late reply, here is the source code


    Thank you!!

  • I can reproduce the behaviour, but unfortnately I don't have any solution. Maybe it's a bug - you should consider reporting it at

  • Thanks, I registered and found the same bug, reported in 2009.
    The ticket was closed with a resolution of "out of scope". So it seems it will not be fixed soon

    But thanks anyway for your help Volker

  • The workaround is simple. The geometry of the parent is available, the geometry of the dialog is also may be obtained.

  • but then you have to use the Qt file dialog, not the Windows-native dialog

  • [quote author="goocreations" date="1291014508"]but then you have to use the Qt file dialog, not the Windows-native dialog[/quote]

    You are right, sorry

Log in to reply