当鼠标经过时 QLabel 改变颜色

QLabel changing color when mouse passed over then

抱歉英语不好,这不是我的母语。 我需要 QT 方面的帮助。 我必须制作一个棋盘,例如棋盘,一个 8 x 8 的方块矩阵。 所以我做了一个继承自 QLabel 的 class,命名为 "Casillero",另一个继承自 QWidget 的板本身名称 "Tablero",我将它作为 centralWidget 添加到主窗口。

我遇到的问题是,当您将鼠标悬停在瓷砖上时,有人将颜色更改为紫色,而有人则没有。我不明白这种行为。

the mouse is over the purple tile

the mouse is over the tile over up on the diagonal of the other picture

class 磁贴名称:"Casillero.h"

class Casillero :public QLabel
{
public:
    Casillero(QWidget* pParent=0, Qt::WindowFlags f=0) : QLabel(pParent, f){};
    Casillero(const QString& text, QWidget* pParent = 0, Qt::WindowFlags f = 0) : QLabel(text, pParent, f){};
    void display(char elem);
    void tileDisplay();
    bool hayPieza;
    int casilleroColor,fil,col,casilleroNum;
    char piezaColor;
};

class 磁贴名称:"Casillero.cpp"

#include "casillero.h"

void Casillero::display(char elem)
{
    this->piezaColor=elem;
    if(this->hayPieza)
    {
        if(elem == 'R')
        {
            this->setPixmap(QPixmap(":/Imagenes/damaroja.png"));
        }
        else
        {
            this->setPixmap(QPixmap(":/Imagenes/damanegra.png"));
        }
    }
    else
        this->clear();
}

void Casillero::tileDisplay()
{
    if(this->casilleroColor)
        this->setStyleSheet("QLabel {background-color: rgb(118, 150, 85);}:hover{background-color: rgb(170,85,127);}");
    else
        this->setStyleSheet("QLabel {background-color: rgb(238, 238, 210);}:hover{background-color: rgb(170,95,127);}");
}

class 板名为:"Tablero.h"

class Tablero :public QWidget
{
public:
    Tablero(QWidget *parent);
private:
    QLabel *bordes[4];
    Casillero *tab[8][8];
};

class 板名为:"Tablero.cbp"

#include "tablero.h"

Tablero::Tablero(QWidget *parent)
{
    this->setParent(parent);
    bordes[0]=new QLabel(this);
    bordes[0]->setGeometry(330,105,552,20);
    bordes[0]->setStyleSheet("QLabel { background-color :rgb(178, 194, 148); color : black; }");
    bordes[1]=new QLabel(this);
    bordes[1]->setGeometry(330,637,552,20);
    bordes[1]->setStyleSheet("QLabel { background-color :rgb(178, 194, 148); color : black; }");
    bordes[2]=new QLabel(this);
    bordes[2]->setGeometry(330,125,20,512);
    bordes[2]->setStyleSheet("QLabel { background-color :rgb(178, 194, 148); color : black; }");
    bordes[3]=new QLabel(this);
    bordes[3]->setGeometry(862,125,20,512);
    bordes[3]->setStyleSheet("QLabel { background-color :rgb(178, 194, 148); color : black; }");
    int i,j,k=0,hor,ver;
    ver=125;
        for(i=0;i<8;i++)
        {
            hor=350;
            for(j=0;j<8;j++)
            {
                tab[i][j] = new Casillero(this);
                tab[i][j]->casilleroColor=(i+j)%2;
                tab[i][j]->hayPieza=false;
                tab[i][j]->fil=i;
                tab[i][j]->col=j;
                tab[i][j]->casilleroNum=k++;
                tab[i][j]->tileDisplay();
                tab[i][j]->setGeometry(hor,ver,64,64);
                hor+=64;
            }
            ver+=64;
        }

        //damas negras
        for(j=1;j<7;j++)
        {
            tab[0][j]->hayPieza=true;
            tab[0][j]->display('N');
            tab[0][j]->piezaColor='N';
            tab[7][j]->hayPieza=true;
            tab[7][j]->display('N');
            tab[7][j]->piezaColor='N';
        }

        //damas rojas
        for(j=1;j<7;j++)
        {
            tab[j][0]->hayPieza=true;
            tab[j][0]->display('R');
            tab[j][0]->piezaColor='R';
            tab[j][7]->hayPieza=true;
            tab[j][7]->display('R');
            tab[j][7]->piezaColor='R';
        }
}

问题是 mainwindow.cpp

中的黑板上有一个小部件
widget = new QWidget(centralWidget);
widget->setObjectName(QStringLiteral("widget"));
widget->setGeometry(QRect(200, 50, 551, 341));