Search code examples
c++qtqwidgetqlabelqmainwindow

QLabel changing color when mouse passed over then


sorry for the bad english, this isn't my first language. i need help with QT. i had to make a board, like chess board an 8 by 8 matrix of tiles. so i made a class that inherits of QLabel named as "Casillero" and another one that is the board itself name "Tablero" that inherits of QWidget and i added it to the mainwindow as centralWidget.

the problem that i have is that when you hover the mouse over the tiles someones change color to purple and someones not. I do not understand this behavior.

the mouse is over the purple tile

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

class tile named:"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 tile named:"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 board named:"Tablero.h"

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

class board named:"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';
        }
}

Solution

  • The problem was that there was a widget over the board in mainwindow.cpp

    widget = new QWidget(centralWidget);
    widget->setObjectName(QStringLiteral("widget"));
    widget->setGeometry(QRect(200, 50, 551, 341));