Results 1 to 3 of 3

Thread: needs to instantiate a dialogue in a switch construct

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    672
    Thanks
    55
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default needs to instantiate a dialogue in a switch construct

    Good morning,
    even if I know that it is not the correct technique to do, I need to instantiate a different type of dialogue depending on the parameter passed on the command line.

    Something like that:

    Qt Code:
    1. enum class FILTERS
    2. {
    3. AIRPORTS = 1,
    4. FDDI = 2,
    5. RRT = 3,
    6. TARGET = 4,
    7. TRACK = 5
    8. };
    9.  
    10. int main (int argc, char ** argv)
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. QMap<QString, FILTERS> filterMap;
    15. filterMap["airports"] = FILTERS::AIRPORTS;
    16. filterMap["fddi"] = FILTERS::FDDI;
    17. filterMap["rrt"] = FILTERS::RRT;
    18. filterMap["target"] = FILTERS::TARGET;
    19. filterMap["track"] = FILTERS::TRACK;
    20.  
    21. QCommandLineParser parser;
    22. parser.setApplicationDescription(QApplication::translate("main", "Call the right filter"));
    23. parser.addHelpOption();
    24.  
    25. QCommandLineOption filterTypeOption("t", QApplication::translate("main", "Specifies the type of filter to call"));
    26. parser.addOption(filterTypeOption);
    27.  
    28. parser.process(app);
    29.  
    30. QString filterType = parser.value(filterTypeOption);
    31. FILTERS filter = filterMap[filterType];
    32.  
    33. switch(filter)
    34. {
    35. case FILTERS::AIRPORTS:
    36. AirportsDialog dialog;
    37. break;
    38.  
    39. case FILTERS::FDDI:
    40. FDDIDialog dialog;
    41. break;
    42.  
    43. case FILTERS::RRT:
    44. RRTDialog dialog;
    45. break;
    46.  
    47. case FILTERS::TARGET:
    48. TargetDialog dialog;
    49. break;
    50.  
    51. case FILTERS::TRACK;
    52. TrackDialog dialog;
    53. break;
    54. }
    55.  
    56. dialog.show();
    57. int ret = app.exec();
    58. return ret;
    59. }
    To copy to clipboard, switch view to plain text mode 

    An example of a call will be:
    dialog_caller -t rrt
    or

    dialog_caller -t airports
    Is there a better way to achieve this behavior? The different dialogs are totally different from each other.
    By instancing the dialog in the switch it will be removed once you exit from the switch?

    How to make it remain active until the end of the program?
    Franco Amato

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    487
    Thanks
    11
    Thanked 71 Times in 69 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: needs to instantiate a dialogue in a switch construct

    Hi, yes, the dialogs will be destroyed when you reach the end of the switch. I would create something like this:
    Qt Code:
    1. QDialog* dialog = NULL;
    2. switch(filter)
    3. {
    4. case FILTERS::AIRPORTS:
    5. dialog = new AirportsDialog ();
    6. break;
    7. // similar for the other cases
    8. ...
    9. }
    10. dialog->show();
    11. // remember to delete the dialog
    To copy to clipboard, switch view to plain text mode 
    Of course this assumes that your dialogs are derived from QDialog.

    Ginsengelf
    Last edited by Ginsengelf; 31st January 2022 at 07:19. Reason: reformatted to look better

  3. The following user says thank you to Ginsengelf for this useful post:

    franco.amato (14th February 2022)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,120
    Thanks
    291
    Thanked 847 Times in 834 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: needs to instantiate a dialogue in a switch construct

    // remember to delete the dialog
    Could do something like this, I think:

    Qt Code:
    1. connect( dialog, &QDialog::finished, dialog, &QDialog::dedleteLater );
    2. dialog->show();
    To copy to clipboard, switch view to plain text mode 

    or use the QDialog::accepted() and QDialog::rejected() signals in the same way.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    franco.amato (14th February 2022)

Similar Threads

  1. cannot instantiate abstract class
    By Higgs in forum Newbie
    Replies: 2
    Last Post: 6th January 2014, 20:41
  2. How to instantiate a class if I dont know its name?
    By anoraxis in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2012, 21:42
  3. Cannot instantiate Plugin with UI
    By trallallero in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2010, 09:34
  4. QAxContainer: can't instantiate control
    By fabien in forum Qt Programming
    Replies: 0
    Last Post: 24th July 2007, 11:22
  5. Replies: 14
    Last Post: 19th March 2007, 08:48

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.