PDA

View Full Version : How to implement simple open file dialog



AirRic
28th April 2013, 21:47
Hello @all,

I spent the last 2 hours searching on Google for a problem that really annoys me, because even in outdated languages/IDEs like Delphi those things are fully implemented within seconds.

Simple task: I just want a QFileDialog to be opened when a QAction in a QMenu is triggered. I'm completely new to Qt, so I first wanted to get familiar with the signal-slot system. Seemed logical to me at first glance: Everytime the QAction is triggered (signal), the QFileDialog pops up (slot).

Now here is my main problem: How is the slot of QFileDialog called which lets it pop up? It would be easier if QFileDialog was an element in Qt Designer and accessable via signal & slot editor. But I can't even find an entry for it in the whole IDE.

Another Google recherche led me to the following code snippet:

QString fileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), "/home", tr("Image Files (*.png *.jpg *.bmp)"));

So this is a piece of code which let a dialog pop up when the application is started. Fine. But I want it to be opened only when the entry in my QMenu is triggered.

One can manually connect a signal to a slot via connect(), but here an ID or name and a slot for the dialog is needed. Or am I completely wrong?

Hopefully, you can help me with this without letting me forget everything I learned about signals and slots so far.

wysota
29th April 2013, 05:51
It seems logical that you would want to do something with the file path returned by the dialog therefore it seems obvious you can't do it as a direct slot invokation. Instead connect the action's triggered() signal to a custom slot where you will call QFileDialog::getOpenFileName() and process the value it returns. And there are really multiple examples in the docs bundled with your Qt installation that show how to do that.