PDA

View Full Version : QLabel background color



munna
1st May 2006, 14:53
Hi,

I have a widget whose background color is set to white using

setBackgroundRole(QPalette::Light);

This widget has a few QLabels on it. These label should change its text color and the background to some other color when mouse over and should have white background and darkGray text when mouse is not over it. For this I have written the following code.



void ViewLabel::enterEvent(QEvent *e)
{
QLabel::enterEvent(e);
QPalette pal;
//setBackgroundRole(QPalette::Dark);
pal.setColor(QPalette::WindowText,QColor(Qt::white ));
pal.setColor(QPalette::Base,QColor(Qt::darkGray));
pal.setColor(QPalette::Window,QColor(Qt::darkGray) );
setPalette(pal);
}

void ViewLabel::leaveEvent(QEvent *e)
{
QLabel::leaveEvent(e);
QPalette pal;
//setBackgroundRole(QPalette::Light);
pal.setColor(QPalette::WindowText,QColor(Qt::darkG ray));
pal.setColor(QPalette::Base,QColor(Qt::white));
pal.setColor(QPalette::Window,QColor(Qt::white));
setPalette(pal);
}


With the above code when mouse goes over the label the background changes to white but the text color is not changing to darkGray. Same thing happens when I use setBackgroundRole.

Can someone please tell me why the text color is not changing ?

Thanks a lot.

zlatko
1st May 2006, 15:06
IMHO it becouse that you set it to white at this line


pal.setColor(QPalette::WindowText,QColor(Qt::white ));

munna
1st May 2006, 15:20
yes the text color is changing to white but the background color is not changing to darkGray.

Why is that so?

Thanks a lot.

jacek
1st May 2006, 15:36
Try adding "setAutoFillBackground( true )".