PDA

View Full Version : TicTacToe, Qt Desktop, Connecting radio buttons



Asus_G72GX
13th March 2012, 17:59
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:
http://s019.radikal.ru/i613/1203/92/7166254d3498.jpg

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

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT

public:
Widget(QWidget *parent = 0);
~Widget();

QGridLayout *grid;
QPushButton *btn1,*btn2, *btn3, *btn4, *btn5, *btn6, *btn7, *btn8, *btn9;
QLabel *xplayer, *oplayer;
QRadioButton *radx1, *rado2;
QLabel *result;


public slots:
void btn1Slot();
void btn2Slot();
void btn3Slot();

void btn4Slot();
void btn5Slot();
void btn6Slot();

void btn7Slot();
void btn8Slot();
void btn9Slot();

void btn1oSlot();
void btn2oSlot();
void btn3oSlot();

void btn4oSlot();
void btn5oSlot();
void btn6oSlot();

void btn7oSlot();
void btn8oSlot();
void btn9oSlot();

};

#endif // WIDGET_H





main.cpp

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}


widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
grid= new QGridLayout;
btn1= new QPushButton;
btn2= new QPushButton;
btn3= new QPushButton;
btn4= new QPushButton;
btn5= new QPushButton;
btn6= new QPushButton;
btn7= new QPushButton;
btn8= new QPushButton;
btn9= new QPushButton;
xplayer =new QLabel;
radx1= new QRadioButton;
oplayer=new QLabel;
rado2= new QRadioButton;
result= new QLabel;

xplayer->setText("X");
oplayer->setText("O");

grid->addWidget(btn1, 0,1);
grid->addWidget(btn2, 0,2);
grid->addWidget(btn3,0,3);
grid->addWidget(btn4,1,1);
grid->addWidget(btn5,1,2);
grid->addWidget(btn6,1,3);
grid->addWidget(btn7,2,1);
grid->addWidget(btn8,2,2);
grid->addWidget(btn9,2,3);

grid->addWidget(xplayer, 3,1);
grid->addWidget(radx1,3,2);

grid->addWidget(oplayer, 4,1);
grid->addWidget(rado2,4,2);
grid->addWidget(result,5,1);



connect(btn1, SIGNAL(clicked(bool)), this, SLOT(btn1Slot()));
connect(btn2, SIGNAL(clicked(bool)), this, SLOT(btn2Slot()));
connect(btn3, SIGNAL(clicked(bool)), this, SLOT(btn3Slot()));

connect(btn4, SIGNAL(clicked(bool)), this, SLOT(btn4Slot()));
connect(btn5, SIGNAL(clicked(bool)), this, SLOT(btn5Slot()));
connect(btn6, SIGNAL(clicked(bool)), this, SLOT(btn6Slot()));

connect(btn7, SIGNAL(clicked(bool)), this, SLOT(btn7Slot()));
connect(btn8, SIGNAL(clicked(bool)), this, SLOT(btn8Slot()));
connect(btn9, SIGNAL(clicked(bool)), this, SLOT(btn9Slot()));

connect(btn1, SIGNAL(clicked(bool)), this, SLOT(btn1oSlot()));
connect(btn2, SIGNAL(clicked(bool)), this, SLOT(btn2oSlot()));
connect(btn3, SIGNAL(clicked(bool)), this, SLOT(btn3oSlot()));

connect(btn4, SIGNAL(clicked(bool)), this, SLOT(btn4oSlot()));
connect(btn5, SIGNAL(clicked(bool)), this, SLOT(btn5oSlot()));
connect(btn6, SIGNAL(clicked(bool)), this, SLOT(btn6oSlot()));

connect(btn7, SIGNAL(clicked(bool)), this, SLOT(btn7oSlot()));
connect(btn8, SIGNAL(clicked(bool)), this, SLOT(btn8oSlot()));
connect(btn9, SIGNAL(clicked(bool)), this, SLOT(btn9oSlot()));

setLayout(grid);

}

Widget::~Widget()
{
}

void Widget::btn1Slot()
{
btn1->setText("X");

}


void Widget::btn2Slot()
{
btn2->setText("X");

}

void Widget::btn3Slot()
{
btn3->setText("X");

}

void Widget::btn4Slot()
{
btn4->setText("X");

}

void Widget::btn5Slot()
{
btn5->setText("X");

}

void Widget::btn6Slot()
{
btn6->setText("X");

}

void Widget::btn7Slot()
{
btn7->setText("X");

}

void Widget::btn8Slot()
{
btn8->setText("X");

}

void Widget::btn9Slot()
{
btn9->setText("X");

}


void Widget::btn1oSlot()
{
btn1->setText("O");

}


void Widget::btn2oSlot()
{
btn2->setText("O");

}


void Widget::btn3oSlot()
{
btn3->setText("O");

}


void Widget::btn4oSlot()
{
btn4->setText("O");

}



void Widget::btn5oSlot()
{
btn5->setText("O");

}


void Widget::btn6oSlot()
{
btn6->setText("O");

}



void Widget::btn7oSlot()
{
btn7->setText("O");

}



void Widget::btn8oSlot()
{
btn8->setText("O");

}


void Widget::btn9oSlot()
{
btn9->setText("O");
}

high_flyer
14th March 2012, 12:12
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:


btn1= new QPushButton;
btn2= new QPushButton;
btn3= new QPushButton;
btn4= new QPushButton;
btn5= new QPushButton;
btn6= new QPushButton;
btn7= new QPushButton;
btn8= new QPushButton;
btn9= new QPushButton;


You can do:


//Where m_vecButtons is declare in the header as: QVector<QPushButton*> m_vecButtons;
for(int iBtn = 0; iBtn<9; ++i){
m_vecButtons.push_back(new QPushButton());
}


You might also want to have a look at QSignalMapper .