仅将方法应用于类的一个对象

applying a Method to only one object of a class

本文关键字:一个对象 应用于 方法      更新时间:2023-10-16

所以我目前正在开发一个简单的战舰版本,在尝试为按钮着色时遇到了问题。

由于有很多按钮,我正在使用两个 for 循环开发它们,将它们添加到 UI 中的网格中。每个按钮都由 x 坐标、y 坐标和玩家编号(1 或 2(标识。

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

    /**
    /  UI Button connects
    **/
     connect(ui->closeButton2,SIGNAL(clicked()),this,SLOT(closeWindow()));

    /**
    / Generates Player 1 Field and sets color to Blue
    **/
    ui->gridLayout1->setVerticalSpacing(0);
    ui->gridLayout1->setHorizontalSpacing(0);

    for(int x = 0 ; x < 10 ; x++)
    {
        for (int y = 0 ; y < 10 ; y++)
        {
            Button *button = new Button( x , y,1);                                                            //New Objects
            ui->gridLayout1->addWidget(button, x, y);                                                       //adds buttons to right Widget
            button->setStyleSheet("background-color: blue");                                                //sets all buttons to color blue
            connect(button,SIGNAL(mySignal(int,int,int)),this,SLOT(sendToLogic(int,int,int)));                      //turns the clicked Signal into a Signal with Coord
            connect(this,SIGNAL(colorGreen(int,int,int)),button,SLOT(green(int,int,int)));                          //connects Widget and Button to color a button Green
            connect(this,SIGNAL(colorRed(int,int,int)),button,SLOT(red(int,int,int)));                              //connects Widget and Button to color a button Red
            connect(this,SIGNAL(colorBlack(int,int,int)),button,SLOT(black(int,int,int)));                          //connects Widget and Button to color a button Black

        }
    }
    /**
    / Generates Player 2 Field and sets color to Blue
    **/
    ui->gridLayout2->setVerticalSpacing(0);
    ui->gridLayout2->setHorizontalSpacing(0);
    for(int x = 0 ; x < 10 ; x++)
    {
        for (int y = 0 ; y < 10; y++)
        {
            Button *button = new Button(x ,y,2);                                                               //New Objects
            ui->gridLayout2->addWidget(button,x,y);                                                          //adds buttons to left Widget
            button->setStyleSheet("background-color: blue");                                                 //sets all buttons to color blue
            connect(button,SIGNAL(mySignal(int,int,int)),this,SLOT(sendToLogic(int,int,int)));                       //turns the clicked Signal into a Signal with Coord
            connect(this,SIGNAL(colorGreen(int,int,int)),button,SLOT(green(int,int,int)));                           //connects Widget and Button to color a button Green
            connect(this,SIGNAL(colorRed(int,int,int)),button,SLOT(red(int,int,int)));                               //connects Widget and Button to color a button Red
            connect(this,SIGNAL(colorBlack(int,int,int)),button,SLOT(black(int,int,int)));                           //connects Widget and Button to color a button Black
            connect(this,SIGNAL(colorGreen(int,int,int)),button,SLOT(green(int,int,int)));                           //connects Widget and Button to color a button Green
        }
    }
}
void Widget::sendToLogic(int x,int y,int player)                               //sends GUI entries to Logic
{
    qDebug()<< "x" << x << "and y " << y << "and player" << player;
    emit GUItoLogic(x,y,player);
}
void Widget::errorMessage()                                         //recieves errorSignal and shows user "invalid Entry"
{
    ui->tbOut->append("invalid Entry.");
}
void Widget::closeWindow()                                          //closes widget window
{
    this->close();
}

void Widget::colorButtonGreen(int x,int y,int player)                          //sends Signal to Button to turn green
{
    emit colorGreen(x,y,player);
}
void Widget::colorButtonRed(int x,int y,int player)                            //sends Signal to Button to turn Red
{
    emit colorRed(x,y,player);
}
void Widget::colorButtonBlack(int x,int y,int player)                          //sends Signal to Button to turn Black
{
    emit colorBlack(x,y,player);
}
Widget::~Widget()
{
    delete ui;
}

每当我单击某个按钮时,代码都会设法打印出右键单击。我遇到的问题是,当我添加着色信号时,所有按钮都会变为该颜色,而不仅仅是我单击的一个按钮。

按钮类:

Button::Button(int _x, int _y,int _player): x(_x),y(_y),player(_player)
{
    connect(this,SIGNAL(clicked()),this,SLOT(emitmySignal()));
}
void Button::emitmySignal()                                                                      // emit ein Signal mit Button Koordinaten, die gedruckt wurden
{
   // setStyleSheet("background-color: red");
   emit mySignal(x,y,player);
   clearFocus();
}
void Button::green(int x, int y,int player)
{
    qDebug()<< "x" << x << "and y " << y << player;
   this->setStyleSheet("background-color: green");
}
void Button::red(int x, int y,int player)
{
    qDebug()<< "x" << x << "and y " << y << player;
    setStyleSheet("background-color: red");
}
void Button::black(int x, int y,int player)
{
    qDebug()<< "x" << x << "and y " << y << player;
  //  setStyleSheet("background-color:black");
}

您的signal已连接到所有按钮。因此,每次触发时,它都会调用所有按钮slot s。