Results 1 to 2 of 2

Thread: TicTacToe, Qt Desktop, Connecting radio buttons

  1. #1
    Join Date
    Mar 2012
    Posts
    9
    Platforms
    Windows Symbian S60

    Exclamation TicTacToe, Qt Desktop, Connecting radio buttons

    Good evening dear Qtccentre.org programmers!

    I am currently in the very early stage of creating tic tac toe game for desktop. I have already created my form, placed buttons on it. But I have no idea how to connect two radiobuttons for my game.
    Here is a screenshot of my game:


    The main idea of it, when users selects the X radio button and clicks to buttons, text "X" should be appear on buttons, and when user select O radio button, text "O" should appear on buttons. I'v tried to use if(rado2->isChecked()) but programm crashes. Please help me to connect this two radiobuttons, so that labels ("X" or "O") could correctly adds to my buttons. Great thanks!

    My code:
    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Widget : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Widget(QWidget *parent = 0);
    12. ~Widget();
    13.  
    14. QGridLayout *grid;
    15. QPushButton *btn1,*btn2, *btn3, *btn4, *btn5, *btn6, *btn7, *btn8, *btn9;
    16. QLabel *xplayer, *oplayer;
    17. QRadioButton *radx1, *rado2;
    18. QLabel *result;
    19.  
    20.  
    21. public slots:
    22. void btn1Slot();
    23. void btn2Slot();
    24. void btn3Slot();
    25.  
    26. void btn4Slot();
    27. void btn5Slot();
    28. void btn6Slot();
    29.  
    30. void btn7Slot();
    31. void btn8Slot();
    32. void btn9Slot();
    33.  
    34. void btn1oSlot();
    35. void btn2oSlot();
    36. void btn3oSlot();
    37.  
    38. void btn4oSlot();
    39. void btn5oSlot();
    40. void btn6oSlot();
    41.  
    42. void btn7oSlot();
    43. void btn8oSlot();
    44. void btn9oSlot();
    45.  
    46. };
    47.  
    48. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 


    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. grid= new QGridLayout;
    7. btn1= new QPushButton;
    8. btn2= new QPushButton;
    9. btn3= new QPushButton;
    10. btn4= new QPushButton;
    11. btn5= new QPushButton;
    12. btn6= new QPushButton;
    13. btn7= new QPushButton;
    14. btn8= new QPushButton;
    15. btn9= new QPushButton;
    16. xplayer =new QLabel;
    17. radx1= new QRadioButton;
    18. oplayer=new QLabel;
    19. rado2= new QRadioButton;
    20. result= new QLabel;
    21.  
    22. xplayer->setText("X");
    23. oplayer->setText("O");
    24.  
    25. grid->addWidget(btn1, 0,1);
    26. grid->addWidget(btn2, 0,2);
    27. grid->addWidget(btn3,0,3);
    28. grid->addWidget(btn4,1,1);
    29. grid->addWidget(btn5,1,2);
    30. grid->addWidget(btn6,1,3);
    31. grid->addWidget(btn7,2,1);
    32. grid->addWidget(btn8,2,2);
    33. grid->addWidget(btn9,2,3);
    34.  
    35. grid->addWidget(xplayer, 3,1);
    36. grid->addWidget(radx1,3,2);
    37.  
    38. grid->addWidget(oplayer, 4,1);
    39. grid->addWidget(rado2,4,2);
    40. grid->addWidget(result,5,1);
    41.  
    42.  
    43.  
    44. connect(btn1, SIGNAL(clicked(bool)), this, SLOT(btn1Slot()));
    45. connect(btn2, SIGNAL(clicked(bool)), this, SLOT(btn2Slot()));
    46. connect(btn3, SIGNAL(clicked(bool)), this, SLOT(btn3Slot()));
    47.  
    48. connect(btn4, SIGNAL(clicked(bool)), this, SLOT(btn4Slot()));
    49. connect(btn5, SIGNAL(clicked(bool)), this, SLOT(btn5Slot()));
    50. connect(btn6, SIGNAL(clicked(bool)), this, SLOT(btn6Slot()));
    51.  
    52. connect(btn7, SIGNAL(clicked(bool)), this, SLOT(btn7Slot()));
    53. connect(btn8, SIGNAL(clicked(bool)), this, SLOT(btn8Slot()));
    54. connect(btn9, SIGNAL(clicked(bool)), this, SLOT(btn9Slot()));
    55.  
    56. connect(btn1, SIGNAL(clicked(bool)), this, SLOT(btn1oSlot()));
    57. connect(btn2, SIGNAL(clicked(bool)), this, SLOT(btn2oSlot()));
    58. connect(btn3, SIGNAL(clicked(bool)), this, SLOT(btn3oSlot()));
    59.  
    60. connect(btn4, SIGNAL(clicked(bool)), this, SLOT(btn4oSlot()));
    61. connect(btn5, SIGNAL(clicked(bool)), this, SLOT(btn5oSlot()));
    62. connect(btn6, SIGNAL(clicked(bool)), this, SLOT(btn6oSlot()));
    63.  
    64. connect(btn7, SIGNAL(clicked(bool)), this, SLOT(btn7oSlot()));
    65. connect(btn8, SIGNAL(clicked(bool)), this, SLOT(btn8oSlot()));
    66. connect(btn9, SIGNAL(clicked(bool)), this, SLOT(btn9oSlot()));
    67.  
    68. setLayout(grid);
    69.  
    70. }
    71.  
    72. Widget::~Widget()
    73. {
    74. }
    75.  
    76. void Widget::btn1Slot()
    77. {
    78. btn1->setText("X");
    79.  
    80. }
    81.  
    82.  
    83. void Widget::btn2Slot()
    84. {
    85. btn2->setText("X");
    86.  
    87. }
    88.  
    89. void Widget::btn3Slot()
    90. {
    91. btn3->setText("X");
    92.  
    93. }
    94.  
    95. void Widget::btn4Slot()
    96. {
    97. btn4->setText("X");
    98.  
    99. }
    100.  
    101. void Widget::btn5Slot()
    102. {
    103. btn5->setText("X");
    104.  
    105. }
    106.  
    107. void Widget::btn6Slot()
    108. {
    109. btn6->setText("X");
    110.  
    111. }
    112.  
    113. void Widget::btn7Slot()
    114. {
    115. btn7->setText("X");
    116.  
    117. }
    118.  
    119. void Widget::btn8Slot()
    120. {
    121. btn8->setText("X");
    122.  
    123. }
    124.  
    125. void Widget::btn9Slot()
    126. {
    127. btn9->setText("X");
    128.  
    129. }
    130.  
    131.  
    132. void Widget::btn1oSlot()
    133. {
    134. btn1->setText("O");
    135.  
    136. }
    137.  
    138.  
    139. void Widget::btn2oSlot()
    140. {
    141. btn2->setText("O");
    142.  
    143. }
    144.  
    145.  
    146. void Widget::btn3oSlot()
    147. {
    148. btn3->setText("O");
    149.  
    150. }
    151.  
    152.  
    153. void Widget::btn4oSlot()
    154. {
    155. btn4->setText("O");
    156.  
    157. }
    158.  
    159.  
    160.  
    161. void Widget::btn5oSlot()
    162. {
    163. btn5->setText("O");
    164.  
    165. }
    166.  
    167.  
    168. void Widget::btn6oSlot()
    169. {
    170. btn6->setText("O");
    171.  
    172. }
    173.  
    174.  
    175.  
    176. void Widget::btn7oSlot()
    177. {
    178. btn7->setText("O");
    179.  
    180. }
    181.  
    182.  
    183.  
    184. void Widget::btn8oSlot()
    185. {
    186. btn8->setText("O");
    187.  
    188. }
    189.  
    190.  
    191. void Widget::btn9oSlot()
    192. {
    193. btn9->setText("O");
    194. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 14th March 2012 at 11:02. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: TicTacToe, Qt Desktop, Connecting radio buttons

    I'v tried to use if(rado2->isChecked()) but programm crashes.
    Where did you try to use it?
    Your code is legal, but very cumbersome, and would not be applicable for any real world application.
    Make your self familiar with loops, array/containers.
    For example, instead of:
    Qt Code:
    1. btn1= new QPushButton;
    2. btn2= new QPushButton;
    3. btn3= new QPushButton;
    4. btn4= new QPushButton;
    5. btn5= new QPushButton;
    6. btn6= new QPushButton;
    7. btn7= new QPushButton;
    8. btn8= new QPushButton;
    9. btn9= new QPushButton;
    To copy to clipboard, switch view to plain text mode 
    You can do:
    Qt Code:
    1. //Where m_vecButtons is declare in the header as: QVector<QPushButton*> m_vecButtons;
    2. for(int iBtn = 0; iBtn<9; ++i){
    3. m_vecButtons.push_back(new QPushButton());
    4. }
    To copy to clipboard, switch view to plain text mode 

    You might also want to have a look at QSignalMapper .
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. QTableWidget radio buttons
    By Pettson in forum Newbie
    Replies: 4
    Last Post: 15th December 2011, 09:51
  2. Non editable Radio Buttons
    By babu198649 in forum Newbie
    Replies: 9
    Last Post: 11th November 2009, 11:48
  3. Replies: 1
    Last Post: 7th August 2007, 08:27
  4. array of radio buttons
    By amulya in forum Qt Programming
    Replies: 4
    Last Post: 5th October 2006, 12:59
  5. How to get larger radio buttons on XP?
    By Ben.Hines in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2006, 19:00

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.