Results 1 to 2 of 2

Thread: The QInputDialog::getText(0, unexpected behaviour ...

  1. #1
    Join Date
    Dec 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default The QInputDialog::getText(0, unexpected behaviour ...

    Dear Forumers, I tryed to get input file name by QInputDialog::getText(0 method and came up with strange behaviour. If after click on OK at the dialog window program didn't returned . I made another click and after that I got what I expected after first click. Why ? In case I clicked on Cancel button - everything is Ok. Could you please to give me hint how to get right behaviour ? Se my code below (I am workin on LINUX Debian Wheezy machine). Thank you in advance for help.
    Qt Code:
    1. #include <QApplication>
    2. #include <QInputDialog>
    3. #include <QDebug>
    4.  
    5. void writ_file();
    6. QString InputFileName();
    7.  
    8. QString file_name = "test.txt";
    9.  
    10. int main(int argc, char **argv){
    11. QApplication app(argc, argv);
    12. writ_file();
    13. return app.exec();
    14. }
    15.  
    16. void writ_file() {
    17.  
    18. if(InputFileName() == "" ) {
    19. qDebug() << "Input file name -> cancelled. Uses the default name : "
    20. << file_name << endl;
    21. }
    22. else file_name = InputFileName();
    23. qDebug() << "writ_file() -> " << file_name;
    24. return;
    25. }
    26.  
    27. QString InputFileName() {
    28.  
    29. bool bOk;
    30. QString FileName = QInputDialog::getText(0,
    31. "Input File Name",
    32. "Name:",
    33. QLineEdit::Normal,
    34. "test.txt",
    35. &bOk
    36. );
    37. if (!bOk) {
    38. // Была нажата кнопка Cancel
    39. return "";
    40. }
    41. qDebug() << FileName ;
    42. //printed Filename after first click (hanging, don't make return)
    43. // and after second click print again and make return
    44. return FileName;
    45. }
    46. // Output if I press OK
    47. // "test.txt"
    48. // "test.txt"
    49. // writ_file() -> "test.txt"
    50. // Output if I press Cancel
    51. // Input file name -> cancelled. Uses the default name : "test.txt"
    52.  
    53. // writ_file() -> "test.txt"
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,403
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: The QInputDialog::getText(0, unexpected behaviour ...

    Well, if you click OK, then InputFileName() will return the filename you have selected.

    Since that filename is not "", you are getting into the else case of your if condition in writ_file, which again calls InputFileName().

    And this of course shows the dialog again and again requires you to click either OK or Cancel.

    I think want you want to do is

    Qt Code:
    1. void writ_file() {
    2. QString fileName = InputFileName();
    3. if(fileName == "" ) {
    4. qDebug() << "Input file name -> cancelled. Uses the default name : "
    5. << file_name << endl;
    6. }
    7. else file_name = fileName;
    8. qDebug() << "writ_file() -> " << file_name;
    9. return;
    10. }
    To copy to clipboard, switch view to plain text mode 

    Btw, instead of returning "" just return QString() and instead of checking == "", check fileName.isEmpty()

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 11th January 2011, 11:27
  2. Replies: 4
    Last Post: 28th August 2010, 05:42
  3. QInputDialog::getText - showing twice
    By TorAn in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2009, 01:57
  4. Replies: 10
    Last Post: 13th September 2008, 02:39
  5. qinputdialog strange behaviour
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2008, 19:29

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.