Results 1 to 6 of 6

Thread: Issue with QFileDialog

  1. #1
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Issue with QFileDialog

    Hi,

    I am using QFilDialog::getOpenFilename() to get the file name chosen by the user. It is working fine for me. But the issue is when the user chose to press the "Cancel" button instead of "Open". When I press the Cancel button I am closing the dialog with close signal. But program get struck after closing the window. I am confused of writing the logic between "open" and cancel" button. My code is

    Qt Code:
    1. QString Directoryfinder::getFilename()
    2. {
    3. QFileDialog *dialog;
    4. QString filepath = dialog->getOpenFileName(this, tr("Open File"),"/home",tr("Text files(*.txt)"));
    5. if(filepath.isNull())
    6. connect(dialog,SIGNAL(rejected()),dialog,SLOT(close()));
    7.  
    8. return filepath;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone please guide me with the logic.

    Thank You,

    baluk

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Issue with QFileDialog

    you might want to initialize your dialog? dialog is a null pointer! and by the way your connect statement doesn't make any sense.

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QFileDialog

    Quote Originally Posted by baluk View Post
    Qt Code:
    1. QString filepath = dialog->getOpenFileName(this, tr("Open File"),"/home",tr("Text files(*.txt)"));
    To copy to clipboard, switch view to plain text mode 
    QFileDialog::getOpenFileName() is a static function. You don't need an instance of QFileDialog for it, which is why this code is properly executed in the first place.

    The following code is (arguably) better:
    Qt Code:
    1. QString file = QFileDialog::getOpenFileName(this, tr("Open File"),"/home",tr("Text files(*.txt)"));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. if(filepath.isNull())
    2. connect(dialog,SIGNAL(rejected()),dialog,SLOT(close()));
    To copy to clipboard, switch view to plain text mode 
    As lykurg said, this doesn't make sense. That is for two reasons:
    • QFileDialog::getOpenFileName() is a static function and blocking function. When the function returns, the file dialog is already closed.
    • dialog is uninitialized

    Qt Code:
    1. return filepath;
    To copy to clipboard, switch view to plain text mode 
    This basically means that your function should have been:
    Qt Code:
    1. QString Directoryfinder::getFilename()
    2. {
    3. return QFileDialog::getOpenFileName(this, tr("Open File"), "/home", tr("Text files (*.txt)");
    4. }
    To copy to clipboard, switch view to plain text mode 

    When the dialog is canceled, the string will be null. When the dialog is accepted, the string will be non-null. Based on that you can make decisions. The signals and slots will be involved as soon as you stop using the static functions and start the open() paradigm.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  4. #4
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue with QFileDialog

    oke now I have initialized the dialog. it works fine. but how can I close the dialog when the user press the "Cancel" button.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Issue with QFileDialog

    Have you tried the static version as franz had suggested? The dialog closes itself when pressing cancel.

  6. #6
    Join Date
    Jun 2010
    Posts
    137
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue with QFileDialog

    Thanks to both of you for detailed explanation. It is working fine now except that I have to press "Cancel" twice before it is closed. It's not an issue but just want to know the reason.

Similar Threads

  1. setViewMode issue of QFileDialog
    By nikhilqt in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2010, 08:50
  2. QFileDialog issue
    By a_m_mukul in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 23:46
  3. QFileDialog Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2007, 04:30
  4. QFileDialog Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2007, 15:37
  5. QFileDialog Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2007, 13:47

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.