Results 1 to 9 of 9

Thread: Making my central code better

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Making my central code better

    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):

    Qt Code:
    1. int decisao;
    2. bool continuaounao = false;
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. NovoMain NM;
    9.  
    10. inicio: decisao = NM.executa();
    11. if (decisao == 1) //Positions
    12. goto spositions;
    13. else if (decisao == 2) //Body
    14. goto body;
    15. else if (decisao == 3) //Outro
    16. goto context;
    17. else if (decisao == 4) //Creditos
    18. goto creditos;
    19. else if (decisao == 0)
    20. return 0;
    21.  
    22. spositions: {
    23. qDebug() << "Positions";
    24. SPositions sp;
    25. sp.setWindowTitle("Positions");
    26. sp.showMaximized();
    27. sp.show();
    28. a.exec();
    29. if (sp.getSaida() == true)
    30. return 0;
    31. else goto inicio;
    32. }
    33.  
    34. body: {
    35. Body bd;
    36. bd.setWindowTitle("Body knowledge");
    37. bd.showMaximized();
    38. bd.show();
    39. a.exec();
    40. if (bd.getSaida() == true)
    41. return 0;
    42. else goto inicio;
    43. }
    44. context: {
    45. Context ct;
    46. ct.setWindowTitle("Context and details");
    47. ct.showMaximized();
    48. ct.show();
    49. a.exec();
    50. if (ct.getSaida() == true)
    51. return 0;
    52. else goto inicio;
    53. }
    54. creditos: {
    55. Credits cd;
    56. cd.setWindowTitle("Credits");
    57. cd.showMaximized();
    58. cd.show();
    59. qDebug() << "Credits";
    60. a.exec();
    61. if (cd.getSaida() == true)
    62. return 0;
    63. else goto inicio;
    64. }
    65. }
    To copy to clipboard, switch view to plain text mode 

    A example of the basic code used in the getSaida() function (the same for all of them):

    Qt Code:
    1. Context::Context(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::Context)
    4. {
    5. ui->setupUi(this);
    6. continua = false;
    7. }
    8.  
    9. Context::~Context()
    10. {
    11. delete ui;
    12. }
    13.  
    14. void Context::on_actionClose_triggered()
    15. {
    16. close();
    17. }
    18.  
    19. void Context::on_actionExit_triggered()
    20. {
    21. continua = true;
    22. close();
    23. }
    24.  
    25. bool Context::getSaida()
    26. {
    27. return continua;
    28. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Making my central code better

    You can start by using a switch statement to eliminate the if() and goto collection.

  3. #3
    Join Date
    Sep 2011
    Posts
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: Making my central code better

    As suggested, use a switch statement. I would also use an enumerator instead of raw integers to improve code readability. Something like:
    Qt Code:
    1. switch (decisao){
    2. case Decisao::Positions:
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Making my central code better

    Nowadays, you would get shot, then hanged, and then crucified for using the 'goto' statement.

    Use loops with terminating conditions and 'switch' instead.

  5. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Making my central code better

    Chris,

    thanks for the typ.

    Quote Originally Posted by bah View Post
    As suggested, use a switch statement. I would also use an enumerator instead of raw integers to improve code readability. Something like:
    Qt Code:
    1. switch (decisao){
    2. case Decisao::Positions:
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 
    bah, could you please give me more details about your suggestion with switch? I know how to use this with int values (which I presume was ChrisW67 idea), but not as your code implements.

    marcvanriet, ok, I understood ^^ Normally I do use your suggestions, but some algorithms that are uncommon to me sometimes make me use goto and so forth.


    Thanks for everyone,

    Momergil.

  6. #6
    Join Date
    Sep 2011
    Posts
    9
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: Making my central code better

    Here's a code sample (there are better enum examples out there), though you might want to change the numbering (I've kept your scheme, though if feels weird starting at 1). The FINAL_DECISAO is there in case you want to use it in a loop somewhere (and should be updated to point to the right enum entry). Also, you should be careful with namespace pollution. Obs: you don't need to explicitly define each value, the enum is supposed to increment by one at each subsequent enum entry (I recommend defining the first one,as some compilers start them at 0 others at 1).

    Qt Code:
    1. namespace Decisao
    2. {
    3. // FINAL_DECISAO must match the last enumerator entry
    4. enum inertiaEnum {
    5. Positions = 1,
    6. Body = 2,
    7. Context = 3,
    8. Creditos = 4,
    9. FINAL_DECISAO = creditos
    10. };
    11. //... whatever else is on this namespace
    12. }
    To copy to clipboard, switch view to plain text mode 

    You can then use the enum as you would use the hard coded int, except you won't need to remember what each number means. It will also allow you to change the numbering without refactoring the entire code.
    Qt Code:
    1. switch (decisao){
    2. case Decisao::Positions:
    3. //do what should be done when Positions is selected
    4. case Decisao::Body:
    5. //do what should be done when Body is selected
    6. case Decisao::Context:
    7. //do what should be done when Context is selected
    8. case Decisao::Creditos
    9. //do what should be done when Creditos is selected
    10. default:
    11. // do what should be done when none of the others is selected
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Qt4 replaced many of Qt3 boolean function parameters for enums so you could actually know what it meant. One such example (I'm pretty sure there was something like this in a blog post about Qt API design somewhere):

    Qt Code:
    1. // Qt4 replacing a case sensitive == false flag for an enumerator
    2. exampleString.replace("%USER%", user, false); // Qt 3
    3. exampleString.replace("%USER%", user, Qt::CaseInsensitive); // Qt 4
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Making my central code better

    And don't forget to 'break':
    Qt Code:
    1. switch (decisao){
    2. case Decisao::Positions:
    3. //do what should be done when Positions is selected
    4. break; // <<<<<<<<<<<<<<<<<<<<<<
    5. case Decisao::Body:
    6. .... etc
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Making my central code better

    In my opinion each case in the switch should just call a method/function that does a particular job instead of embedding the code directly in the switch.

    Qt Code:
    1. while(!endOfProgram) {
    2. int decisiao = NM.executa();
    3. switch(decisiao) {
    4. case Positions: endOfProgram = executePositions(); break;
    5. case Body: endOfProgram = executeBody(); break;
    6. // ...
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then each method can return true or false depending on whether the application should exit the loop or not.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Making my central code better

    Hello everyone!

    Thanks for the help. I used almost all of the typs you gave me and now the code is running fine and, of course, it is much more organized.

    Here it is:

    Qt Code:
    1. int decisao;
    2. bool continuaounao = false;
    3.  
    4. namespace Decisao
    5. {
    6. enum inertiaEnum
    7. {
    8. Positions = 1,
    9. Body = 2,
    10. Context = 3,
    11. Creditos = 4
    12. };
    13. }
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QApplication a(argc, argv);
    18.  
    19. NovoMain NM;
    20.  
    21. while (continuaounao == false)
    22. {
    23. decisao = NM.executa();
    24.  
    25. switch (decisao)
    26. {
    27. case Decisao::Positions:
    28. {
    29. SPositions sp;
    30. sp.setWindowTitle("Positions");
    31. sp.showMaximized();
    32. sp.show();
    33. a.exec();
    34. continuaounao = sp.getSaida();
    35. }
    36. break;
    37.  
    38. case Decisao::Body:
    39. {
    40. Body bd;
    41. bd.setWindowTitle("Body knowledge");
    42. bd.showMaximized();
    43. bd.show();
    44. a.exec();
    45. continuaounao = bd.getSaida();
    46. }
    47. break;
    48.  
    49. case Decisao::Context:
    50. {
    51. Context ct;
    52. ct.setWindowTitle("Context and details");
    53. ct.showMaximized();
    54. ct.show();
    55. a.exec();
    56. continuaounao = ct.getSaida();
    57. }
    58. break;
    59.  
    60. case Decisao::Creditos:
    61. {
    62. Credits cd;
    63. cd.setWindowTitle("Credits");
    64. cd.showMaximized();
    65. cd.show();
    66. a.exec();
    67. continuaounao = cd.getSaida();
    68. }
    69. break;
    70.  
    71. default:
    72. return 0;
    73. break;
    74. }
    75. }
    76. return 0;
    77. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for everyone!


    God bless,

    Momergil

Similar Threads

  1. Qt Designer About central widget in Qt Designer
    By lrjdragon in forum Qt Tools
    Replies: 2
    Last Post: 3rd April 2010, 16:11
  2. Smart Code completion , making default
    By udit in forum Qt Tools
    Replies: 1
    Last Post: 14th September 2009, 10:08
  3. changes in code not making any difference in output
    By sh123 in forum Qt Programming
    Replies: 13
    Last Post: 10th January 2009, 08:44
  4. QDockWidgets without central widget
    By JoeMerchant in forum Qt Programming
    Replies: 17
    Last Post: 8th August 2007, 14:52
  5. Making source code available
    By vermarajeev in forum General Discussion
    Replies: 1
    Last Post: 29th June 2007, 22:01

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
  •  
Qt is a trademark of The Qt Company.