仅将方法应用于类的一个对象
applying a Method to only one object of a class
所以我目前正在开发一个简单的战舰版本,在尝试为按钮着色时遇到了问题。
由于有很多按钮,我正在使用两个 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。
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 使用共享指针的函数调用,其对象应为 const
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 函数可以应用于 std::optional,并返回一个可选值吗?
- Strcmp 类行为应用于C++中的对象列表
- 将对象方法从向量应用于C 的第二个向量中的对象
- 如何将片段着色器应用于OpenGL中的一个对象
- 为什么C2662错误没有't应用于对std::unique_ptr<B>对象
- 自定义内存管理,用于在文件中每行创建一个对象
- 当用于初始化另一个对象时,为什么要按值传递参数
- 一个宏,用于根据参数的数量选择对象实例化
- 应用于const和非const对象的引用返回方法
- 在堆栈程序中实现一个计数器,用于计算对象移动的次数
- 是一个函数,用于转换复制返回值的对象