Results 1 to 2 of 2

Thread: How to implement simple open file dialog

  1. #1
    Join Date
    Apr 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How to implement simple open file dialog

    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:
    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(this,
    2. tr("Open Image"), "/home", tr("Image Files (*.png *.jpg *.bmp)"));
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to implement simple open file dialog

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 19th October 2011, 13:08
  2. Open File Dialog Help
    By "BumbleBee" in forum Newbie
    Replies: 6
    Last Post: 20th February 2011, 10:46
  3. Replies: 31
    Last Post: 11th January 2011, 09:36
  4. Replies: 1
    Last Post: 3rd August 2010, 02:41
  5. File Open dialog with preview?
    By will49 in forum Qt Programming
    Replies: 2
    Last Post: 24th July 2007, 18:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.