PDA

View Full Version : needs to instantiate a dialogue in a switch construct



franco.amato
31st January 2022, 04:19
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:


enum class FILTERS
{
AIRPORTS = 1,
FDDI = 2,
RRT = 3,
TARGET = 4,
TRACK = 5
};

int main (int argc, char ** argv)
{
QApplication app(argc, argv);

QMap<QString, FILTERS> filterMap;
filterMap["airports"] = FILTERS::AIRPORTS;
filterMap["fddi"] = FILTERS::FDDI;
filterMap["rrt"] = FILTERS::RRT;
filterMap["target"] = FILTERS::TARGET;
filterMap["track"] = FILTERS::TRACK;

QCommandLineParser parser;
parser.setApplicationDescription(QApplication::tra nslate("main", "Call the right filter"));
parser.addHelpOption();

QCommandLineOption filterTypeOption("t", QApplication::translate("main", "Specifies the type of filter to call"));
parser.addOption(filterTypeOption);

parser.process(app);

QString filterType = parser.value(filterTypeOption);
FILTERS filter = filterMap[filterType];

switch(filter)
{
case FILTERS::AIRPORTS:
AirportsDialog dialog;
break;

case FILTERS::FDDI:
FDDIDialog dialog;
break;

case FILTERS::RRT:
RRTDialog dialog;
break;

case FILTERS::TARGET:
TargetDialog dialog;
break;

case FILTERS::TRACK;
TrackDialog dialog;
break;
}

dialog.show();
int ret = app.exec();
return ret;
}

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?

Ginsengelf
31st January 2022, 07:18
Hi, yes, the dialogs will be destroyed when you reach the end of the switch. I would create something like this:


QDialog* dialog = NULL;
switch(filter)
{
case FILTERS::AIRPORTS:
dialog = new AirportsDialog ();
break;
// similar for the other cases
...
}
dialog->show();
// remember to delete the dialog

Of course this assumes that your dialogs are derived from QDialog.

Ginsengelf

d_stranz
31st January 2022, 15:44
// remember to delete the dialog

Could do something like this, I think:



connect( dialog, &QDialog::finished, dialog, &QDialog::dedleteLater );
dialog->show();


or use the QDialog::accepted() and QDialog::rejected() signals in the same way.