所以我目前正在开发一个简单版本的战舰,我在尝试为按钮着色时遇到了问题。
由于有许多按钮,我正在使用两个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");
}
答案 0 :(得分:0)
您的signal
已连接到所有按钮。所以每次触发它都会调用所有按钮slot
。