Home > Software design >  Backgammon/Table game in Qt Creator ,c , how can i track the clicked buttons?
Backgammon/Table game in Qt Creator ,c , how can i track the clicked buttons?

Time:11-22

im trying to programm Backgammon in Qt Creator using c ,

Backgammonboard Screenshot from Ui

This is what i got until now, i created diverse Qgroupboxes for example grp_b1, here i have now 5 Buttons wich are the black figures in the upper corner.

    grid = new QGridLayout();
    ui->grp_b1->setLayout(grid);

    feld1=new QButtonGroup;

    feld1_buttons[0]=ui->b1;
    feld1_buttons[1]=ui->b2;
    feld1_buttons[2]=ui->b3;
    feld1_buttons[3]=ui->b4;
    feld1_buttons[4]=ui->b5;

    for(int i=0;i<5;i  )
        {
            feld1->addButton(feld1_buttons[i],i);
            grid->addWidget(feld1_buttons[i]);

I want to ask for help to develop the game logic, i dont know how to track the clicked button.

    QObject::connect(feld1,&QButtonGroup::idClicked,this,&MainWindow::black_figure_group_clicked);

So when i click on the button i get their id, but how could i know wich field is actually being clicked? Because i have many fields, and i can only know wich id inside each field is being clicked but not the field.

I would be very thankfull for any help or suggestion.

CodePudding user response:

You can call method sender() in your slot to get a pointer of call object. After that get pointer parent to get widget field.

Example:

QObject* obj = sender();
QObject* parent =obj ->parent();

And make static cast to widget

  •  Tags:  
  • c qt
  • Related