对不起,英语不好,这不是我的第一语言。我需要QT的帮助。我不得不做一个棋盘,就像国际象棋棋盘一样,一块8×8的瓷砖。因此,我创建了一个继承QLabel的类,名为"Casillero“,另一个类是董事会本身名为"Tablero”的类,它继承了QWidget,并将其添加到主窗口,名为centralWidget。
我的问题是,当你把鼠标悬停在瓷砖上时,有人会把颜色变成紫色,而有些人则不会。我不明白这种行为。
名为“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;
};类瓷砖名:“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);}");
}类董事会命名为:“Tablero.h”
class Tablero :public QWidget
{
public:
Tablero(QWidget *parent);
private:
QLabel *bordes[4];
Casillero *tab[8][8];
};类董事会命名为:“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';
}
}发布于 2018-07-28 18:25:25
问题是在mainwindow.cpp中有一个小部件在板上
widget = new QWidget(centralWidget);
widget->setObjectName(QStringLiteral("widget"));
widget->setGeometry(QRect(200, 50, 551, 341));https://stackoverflow.com/questions/51573757
复制相似问题