PDA

View Full Version : Invoking a QFileDialog from another dialog



skimpax
24th March 2010, 17:13
Hi,

I want to invoke a FileDialog from another dialog window :

I am in my tool options configuration dialog box (which works in a classic manner, with Accept/Cancel buttons which respectively accept new configuration or cancel changes). In one of this, I need to make user select a directory, so I want to invoke QFileDialog for this.
But system hangs at runtime, due to modal aspects.

Any idea to launch QFileDialog from a dialog ?

BR

JohannesMunk
24th March 2010, 17:50
Hi there!

I was curious if I could reproduce this problem:



project file:

TEMPLATE = app
TARGET = modalmodal
SOURCES += main.cpp
HEADERS += forms.h

forms.h

#ifndef FORMS_H
#define FORMS_H

#include <QtGui>
#include <QDebug>

class OptionDialog : public QDialog
{ Q_OBJECT
public:
OptionDialog() {
setModal(true);
setGeometry(100,100,320,240);
setAttribute(Qt::WA_DeleteOnClose,true);
button = new QPushButton("Open File ..",this);
connect(button,SIGNAL(clicked()),this,SLOT(openFil eDialog()));
}
protected slots:
void openFileDialog() {
QString fileName = QFileDialog::getOpenFileName(this, "Test Dialog", "", "Zip-File (*.zip)");
qDebug() << fileName;
}
private:
QPushButton* button;
};

class MainForm : public QWidget
{ Q_OBJECT
public:
MainForm() {
setGeometry(50,50,640,480);
button = new QPushButton("Option Dialog",this);
connect(button,SIGNAL(clicked()),this,SLOT(showOpt ionDlg()));
}
protected slots:
void showOptionDlg() {
OptionDialog* opt = new OptionDialog();
opt->show();
}
private:
QPushButton* button;
};

#endif // FORMS_H

main.cpp:

#include <QtGui>
#include <QApplication>
#include "forms.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainForm mainform;
mainform.show();

app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
return app.exec();
}

This works without any problems..

Did you maybe miss to pass the modal option dialog as parent to the FileDialog?

Johannes

skimpax
24th March 2010, 18:16
OK, thanks to your example, I understood the error : It was related to modal as you suggested.
Many thanks.