Momergil
11th September 2011, 01:38
Hello!
I have a doubt that is not truuuly problematic, but nevertheless would certainly increase my knowledge in programming and make a code of some of my softwares better.
The idea that I have in mind (and I do have a code that makes that work) is that sometimes I want a window (QDialog, normally) to appear before the actual software is shown, or else I want a window (QDialog) to appear where the user will select between a number of MainWindows to use. The first case has a very common example: imagine a software that all times, before it runs, shows a QDialog for the user to put a password in order to use this software. For the second case, here is the code that I'm using to make that work: (since both codes are very similar, I'm gonna show just this second):
int decisao;
bool continuaounao = false;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NovoMain NM;
inicio: decisao = NM.executa();
if (decisao == 1) //Positions
goto spositions;
else if (decisao == 2) //Body
goto body;
else if (decisao == 3) //Outro
goto context;
else if (decisao == 4) //Creditos
goto creditos;
else if (decisao == 0)
return 0;
spositions: {
qDebug() << "Positions";
SPositions sp;
sp.setWindowTitle("Positions");
sp.showMaximized();
sp.show();
a.exec();
if (sp.getSaida() == true)
return 0;
else goto inicio;
}
body: {
Body bd;
bd.setWindowTitle("Body knowledge");
bd.showMaximized();
bd.show();
a.exec();
if (bd.getSaida() == true)
return 0;
else goto inicio;
}
context: {
Context ct;
ct.setWindowTitle("Context and details");
ct.showMaximized();
ct.show();
a.exec();
if (ct.getSaida() == true)
return 0;
else goto inicio;
}
creditos: {
Credits cd;
cd.setWindowTitle("Credits");
cd.showMaximized();
cd.show();
qDebug() << "Credits";
a.exec();
if (cd.getSaida() == true)
return 0;
else goto inicio;
}
}
A example of the basic code used in the getSaida() function (the same for all of them):
Context::Context(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Context)
{
ui->setupUi(this);
continua = false;
}
Context::~Context()
{
delete ui;
}
void Context::on_actionClose_triggered()
{
close();
}
void Context::on_actionExit_triggered()
{
continua = true;
close();
}
bool Context::getSaida()
{
return continua;
}
Note: the NovoMain is a QDialog with a respective QPushButton for each of the possible MainWindow in the code (SPosition, Body, etc.) and, when one of the QPushButton is clicked, it closes the QDialog and return a specific int catch by "decisao".
My question is: is there a better way to do this? Is there a function or something like that that can replace my goto methodology??
Thanks!
Momergil
I have a doubt that is not truuuly problematic, but nevertheless would certainly increase my knowledge in programming and make a code of some of my softwares better.
The idea that I have in mind (and I do have a code that makes that work) is that sometimes I want a window (QDialog, normally) to appear before the actual software is shown, or else I want a window (QDialog) to appear where the user will select between a number of MainWindows to use. The first case has a very common example: imagine a software that all times, before it runs, shows a QDialog for the user to put a password in order to use this software. For the second case, here is the code that I'm using to make that work: (since both codes are very similar, I'm gonna show just this second):
int decisao;
bool continuaounao = false;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
NovoMain NM;
inicio: decisao = NM.executa();
if (decisao == 1) //Positions
goto spositions;
else if (decisao == 2) //Body
goto body;
else if (decisao == 3) //Outro
goto context;
else if (decisao == 4) //Creditos
goto creditos;
else if (decisao == 0)
return 0;
spositions: {
qDebug() << "Positions";
SPositions sp;
sp.setWindowTitle("Positions");
sp.showMaximized();
sp.show();
a.exec();
if (sp.getSaida() == true)
return 0;
else goto inicio;
}
body: {
Body bd;
bd.setWindowTitle("Body knowledge");
bd.showMaximized();
bd.show();
a.exec();
if (bd.getSaida() == true)
return 0;
else goto inicio;
}
context: {
Context ct;
ct.setWindowTitle("Context and details");
ct.showMaximized();
ct.show();
a.exec();
if (ct.getSaida() == true)
return 0;
else goto inicio;
}
creditos: {
Credits cd;
cd.setWindowTitle("Credits");
cd.showMaximized();
cd.show();
qDebug() << "Credits";
a.exec();
if (cd.getSaida() == true)
return 0;
else goto inicio;
}
}
A example of the basic code used in the getSaida() function (the same for all of them):
Context::Context(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Context)
{
ui->setupUi(this);
continua = false;
}
Context::~Context()
{
delete ui;
}
void Context::on_actionClose_triggered()
{
close();
}
void Context::on_actionExit_triggered()
{
continua = true;
close();
}
bool Context::getSaida()
{
return continua;
}
Note: the NovoMain is a QDialog with a respective QPushButton for each of the possible MainWindow in the code (SPosition, Body, etc.) and, when one of the QPushButton is clicked, it closes the QDialog and return a specific int catch by "decisao".
My question is: is there a better way to do this? Is there a function or something like that that can replace my goto methodology??
Thanks!
Momergil