PDA

View Full Version : Applying a "selected" style



Cruz
15th January 2009, 22:32
I have a little style sheet, that I load from a file in the main class and apply it to the QApp like this:



QApplication a(argc, argv);

QFile file("styles.css");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
a.setStyleSheet(styleSheet);

return a.exec();


It's very convenient, because I can change the looks of my application without even having to recompile the code. Now here is the thing. I have QFrames on my gui, that I want to highlight when the mouse moves them and keep highlighted when they get clicked. So I need to set some kind of attribute in the code that the style sheet reacts to.

I have seen this thing in a style sheet before:



.selected {
border-width: 3px;
}


and I figure it would apply to items that somehow get a "selected" attribute. Does anyone know how to do this?

Thanks
Cruz

nifei
16th January 2009, 02:04
i think you can use this to highlight your frame when it's hovered:
QFrame:hover
{
//some style sheet
}

and QFrame does not seem to have selected property, you can add a property to it in code (when a frame is selected, re-implement the QFrame::mouseEvent(...) or install an event filter to the frame)


// when your frame is selected
//in code
( your frame calls ) setProperty("isSelected", true);
( also your frame calls) setStyle(" ");//this is needed to make it work, i don't know why
// when unselected
(your frame calls) setProperty("isSelected", false);
( also your frame calls) setStyle(" ");//this is needed to make it work, i don't know why

//in style sheet:


QFrame[isSelected="true"]
{
// style when selected
}

QFrame[isSelected="false"]
{
//style when unselected
}

Hope this is useful for you.

Cruz
16th January 2009, 08:46
Yes it does have hover! Works like a charm, thank you!