2 Replies - 1085 Views - Last Post: 15 February 2012 - 04:04 AM Rate Topic: -----

#1 patzzzssay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-February 12

C++ Memory Game, Weird Problem

Posted 14 February 2012 - 11:23 AM

Hello guys,
I'm trying to make a memory game GUI application in C++ for school. This is the code I have so far:

Widget Header:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSignalMapper>
#include <QPushButton>

namespace Ui {
    class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:

    void on_btnLogin_clicked();

    void on_btnOffline_clicked();

    void on_btnSingle_clicked();

    void on_btnMulti_clicked();

    void on_btn4x4_clicked();

    void on_btn6x4_clicked();

    void on_btn6x6_clicked();

    void myEventHandler(QString);

private:
    Ui::Widget *ui;
    static const int BTNSIX=6;
    static const int BTNFOUR=4;
    QPushButton* FourFour[BTNFOUR][BTNFOUR]; //button arrays, lines + rows
    QPushButton* SixFour[BTNSIX][BTNFOUR];
    QPushButton* SixSix[BTNSIX][BTNSIX];
    QSignalMapper *mySignalMapper;
};

#endif // WIDGET_H


Widget.cpp
#include "widget.h"
#include "ui_widget.h"

QString text;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentWidget(ui->swLogin);

        mySignalMapper = new QSignalMapper(this); //creating a new signal mapper


            for (int x=0; x<BTNFOUR; x++) {  //creating the 4x4 field with 16 buttons
            for (int y=0; y<BTNFOUR; y++) {
              FourFour[y][x] = new QPushButton(ui->swFourFour);
              text = QString::number(x)+","+QString::number(y);
              FourFour[y][x]->setText("I am Nr. ("+ text +")");
              FourFour[y][x]->setGeometry(0+x*100,0+y*100,100,100);
              connect(FourFour[y][x],SIGNAL(clicked()),mySignalMapper,SLOT(map()));
              mySignalMapper->setMapping(FourFour[y][x],text);
            }
          }

            for (int x=0; x<BTNSIX; x++) { //creating the 6x4 field with 24 buttons
            for (int y=0; y<BTNFOUR; y++) {
              SixFour[y][x] = new QPushButton(ui->swSixFour);
              text = QString::number(x)+","+QString::number(y);
              SixFour[y][x]->setText("I am Nr. ("+ text +")");
              SixFour[y][x]->setGeometry(0+x*100,0+y*100,100,100);
              connect(FourFour[y][x],SIGNAL(clicked()),mySignalMapper,SLOT(map()));
              mySignalMapper->setMapping(FourFour[y][x],text);
            }
          }

            for (int x=0; x<BTNSIX; x++) { //creating the 6x6 field with 36 buttons
              for (int y=0; y<BTNSIX; y++) {
                SixSix[y][x] = new QPushButton(ui->swSixSix);
                text = QString::number(x)+","+QString::number(y);
                SixSix[y][x]->setText("I am Nr. ("+ text +")");
                SixSix[y][x]->setGeometry(0+x*100,0+y*100,100,100);
                connect(SixSix[y][x],SIGNAL(clicked()),mySignalMapper,SLOT(map()));
                mySignalMapper->setMapping(SixSix[y][x],text);
              }
            }                     
            connect(mySignalMapper, SIGNAL(mapped(QString)),this, SLOT(myEventHandler(QString)));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_btnLogin_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swModus); //not important, its just for switching the current widget
}

void Widget::on_btnOffline_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swLevel);
}

void Widget::on_btnSingle_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swLevel);
}

void Widget::on_btnMulti_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swSixSix);
    QWidget::setGeometry(600,200,1200,600); // not  important, just changing the size of the widget
}

void Widget::on_btn4x4_clicked()
{

    ui->stackedWidget->setCurrentWidget(ui->swFourFour);
    QWidget::setGeometry(400,200,800,400);

}

void Widget::on_btn6x4_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swSixFour);
    QWidget::setGeometry(400,200,1200,400);
}

void Widget::on_btn6x6_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->swSixSix);
    QWidget::setGeometry(600,200,1200,600);
}

void Widget::meinEventHandler(QString signal)
{
    QString signal2 = signal;               [color="#FF0000"][b]//Important lines![/b][/color]
    int x = signal.remove(1,2).toInt(); 
    int y = signal2.remove(0,2).toInt();
    FourFour[y][x]->setStyleSheet("background-color: rgb(255, 0, 0);");
    SixFour[y][x]->setStyleSheet("background-color: rgb(255, 0, 0);");
    SixSix[y][x]->setStyleSheet("background-color: rgb(255, 0, 0);");
}




As you might have noticed, it's not playable yet. First of all, I wanted to generate 3 different memory game fields 4x4, 6x4, 6x6 and generate dynamic buttons on them. This is already working. I have 3 different fields. One of them with 16 Buttons, the other one with 24 and the last one with 36 buttons. Now, what I'm trying to do is, letting them react on clicks and change their background colour to red (I'll add pictures later). The 4x4 field is working perfectly. Every button changes it's backgroundcolour to red once I click it. But the 6x4 field and the 6x6 field aren't working. The last 10 buttons of the 6x6 field arent working. The programm just crashes, without any error or something. It just says programm doesn't react anymore. And there are only 2 Buttons working on the 6x4 field, and I just cannot find the problem.

Important lines explanation:
What I'm doing there is basically just finding out which button was pressed and colour it red. So once i click the button [0][0](top left), I want it to become red. That's why i make 2 new variables(x,y) and give both of them one of those values(0,0). Then FourFour[y][x] will be FourFour[0][0] and it will colour the background red for this button. As I said everything is working for the 4x4 field. But the other fields have problems and I don't know why. Would be cool if I get some help here! :)

Cheers.

PS: Sorry for this wall-of-text, lol.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Memory Game, Weird Problem

#2 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: C++ Memory Game, Weird Problem

Posted 14 February 2012 - 04:06 PM

just curious but on line 34, shouldnt
mySignalMapper->setMapping(FourFour[y][x],text); 



be changed to
mySignalMapper->setMapping(SixFour[y][x],text);



and the same with line 33
connect(FourFour[y][x],SIGNAL(clicked()),mySignalMapper,SLOT(map()));


This post has been edited by Crazy_Learner: 14 February 2012 - 04:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 patzzzssay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-February 12

Re: C++ Memory Game, Weird Problem

Posted 15 February 2012 - 04:04 AM

View PostCrazy_Learner, on 14 February 2012 - 11:06 PM, said:

just curious but on line 34, shouldnt
mySignalMapper->setMapping(FourFour[y][x],text); 



be changed to
mySignalMapper->setMapping(SixFour[y][x],text);



and the same with line 33
connect(FourFour[y][x],SIGNAL(clicked()),mySignalMapper,SLOT(map()));



this doesnt fix the problem. someone in another forum told me that theres a problem with the FourFour array, because when I click the button 5,5 in the 6x6 field it crashes. But I don't know how to fix that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1