1

Why the opened dialog is not centered to the main window?

void MainWindow::on_FileOpenAction_triggered()
{
    QStringList fileNames = QFileDialog::getOpenFileNames(
        this, 
        "Open Image",
        QApplication::applicationDirPath(), 
        "Images (*.jpg);;All Files (*.*)"
    );
}

The documentation says that is should work:

This function creates a modal file dialog with the given parent widget. If parent is not 0, the dialog will be shown centered over the parent widget.

I use QT 4.6.3 on Windows XP SP2.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
Gad D Lord
  • 6,620
  • 12
  • 60
  • 106

2 Answers2

1

Also in the documentation is the following:

On Windows the dialog will spin a blocking modal event loop that will not dispatch any QTimers, and if parent is not 0 then it will position the dialog just below the parent's title bar.

Is this what is happening? If not, I would guess that you've found a bug in Qt.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • This is exactly what is happening. Is there a way to avoid it. I guess I should experiment constructing the dialog instead of using the hendy helper getOpenFileNames() routine. – Gad D Lord Aug 24 '10 at 14:50
  • @Gad D Lord: The only way I know of would be skipping the helper routine and positioning the dialog yourself. – Caleb Huitt - cjhuitt Aug 25 '10 at 16:51
0

There is either a bug in the Qt or an error in the documentation. If you want a centered dialog, avoid the static functions and create dialog this way:

QFileDialog dialog(
    this, 
    "Open Image",
    QApplication::applicationDirPath(),
    "Images (*.jpg);;All Files (*.*)");
dialog.setFileMode(QFileDialog::ExistingFiles);
QStringList fileNames;
if (dialog.exec())
    fileNames = dialog.selectedFiles();

Note that this uses the Qt's file dialog and not the native file dialog.