Results 1 to 3 of 3

Thread: My class isnt working properly and i dont know why (Newbie)

  1. #1
    Join Date
    Apr 2020
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy My class isnt working properly and i dont know why (Newbie)

    Hi!
    Im new to Qt and im trying to make a class, that will create a board of buttons, depending on a user's choice but it doesnt work properly and i dont know why...

    here is my code

    rysuj_plansze.h

    Qt Code:
    1. #ifndef RYSUJ_PLANSZE_H
    2. #define RYSUJ_PLANSZE_H
    3.  
    4. #include <QPushButton>
    5. #include <QGridLayout>
    6.  
    7.  
    8. class rysuj_plansze
    9. {
    10. public:
    11. void rysuj();
    12. private:
    13. QPushButton *button;
    14. };
    To copy to clipboard, switch view to plain text mode 


    rysuj_plansze.cpp

    Qt Code:
    1. #include "rysuj_plansze.h"
    2. #include <QApplication>
    3. #include <QLabel>
    4. #include <QtWidgets>
    5. #include <QtCore>
    6. #include <QMessageBox>
    7. #include <QDialog>
    8.  
    9. void rysuj_plansze::rysuj()
    10. {
    11.  
    12. QWidget *window = new QWidget;
    13. QGridLayout *layout = new QGridLayout;
    14. window->setWindowTitle("TIc-Tac-Toe");
    15.  
    16.  
    17.  
    18. QMessageBox msgBox;
    19. msgBox.addButton("3na3", QMessageBox::YesRole);
    20. msgBox.addButton("5na5", QMessageBox::NoRole);
    21. msgBox.setIcon(QMessageBox::Question);
    22. msgBox.setText("Which board do you chose?");
    23. msgBox.exec();
    24.  
    25.  
    26.  
    27.  
    28. if(msgBox.exec() == QMessageBox::Yes)
    29. {
    30. int width = 3;
    31. int height = 3;
    32.  
    33. for(int i = 0; i<height; i++)
    34. {
    35. for(int j = 0; j<width; j++)
    36. {
    37. QPushButton *button = new QPushButton;
    38. layout->addWidget(button,i,j);
    39. }
    40. }
    41. }
    42.  
    43.  
    44.  
    45. else if(msgBox.exec() == QMessageBox::No)
    46. {
    47. int width = 20;
    48. int height = 20;
    49.  
    50. for(int i = 0; i<height; i++)
    51. {
    52. for(int j = 0; j<width; j++)
    53. {
    54. QPushButton *button = new QPushButton;
    55. layout->addWidget(button,i,j);
    56. }
    57. }
    58. }
    59.  
    60.  
    61.  
    62. window->setLayout(layout);
    63. window->show();
    64. }
    To copy to clipboard, switch view to plain text mode 




    main.cpp

    Qt Code:
    1. #include "rysuj_plansze.h"
    2. #include <QApplication>
    3. #include <QLabel>
    4. #include <QtWidgets>
    5. #include <QtCore>
    6. #include <QDialog>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12.  
    13. rysuj_plansze R;
    14. R.rysuj();
    15.  
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 



    When i try to run it in main.cpp the messagebox is displayed multiple times (instead of being displayed once) and in the end the window with the board is empty(instead of being 3on3 or 20on20)
    Last edited by 22eragon22; 20th April 2020 at 23:18.

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

    Default Re: My class isnt working properly and i dont know why (Newbie)

    Hi, you call QMessageBox::exec() multiple times:
    line 23: msgBox.exec();
    line 28: if(msgBox.exec() == QMessageBox::Yes)
    line 45: else if(msgBox.exec() == QMessageBox::No)
    Each time the messagebox is shown. Call exec() only once and store the return value for use in the if() and else if() lines, or use switch-case.

    Ginsengelf

  3. #3
    Join Date
    Apr 2020
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: My class isnt working properly and i dont know why (Newbie)

    Thank You! its working rn!!!

Similar Threads

  1. How to instantiate a class if I dont know its name?
    By anoraxis in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2012, 22:42
  2. Replies: 18
    Last Post: 11th March 2011, 14:21
  3. Replies: 7
    Last Post: 2nd March 2011, 00:02
  4. Replies: 3
    Last Post: 3rd November 2010, 18:30
  5. Why isnt MyItemDelegate working??? please help!
    By Slewman in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2010, 20:22

Tags for this Thread

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.