PDA

View Full Version : Dynamic property and stylesheets



aamer4yu
17th November 2009, 14:02
I need to set border color based on some dynamic property and am using the following stylesheet for the same -


QWidget[editFocus="true"]
{
border: 3px solid rgb(20,15,255);
}
Now if I set the dynamic property 'editFocus' through designer it loads properly the first time. This is obvious.

But when I change the value of "editFocus" through code at runtime, the color doesnt get updated. Is it the correct behaviour ?

What I want to achieve is the border color should change on the widget having focus or edit Focus. This is similar to the edit-focus for Qt for Embedded. But since normal applications dont have such kind of behaviour, am trying to do it on my own.

wysota
17th November 2009, 16:58
But when I change the value of "editFocus" through code at runtime, the color doesnt get updated. Is it the correct behaviour ?
Yes. The stylesheet needs to be reapplied if you want it to reflect changes to the variable.


What I want to achieve is the border color should change on the widget having focus or edit Focus. This is similar to the edit-focus for Qt for Embedded. But since normal applications dont have such kind of behaviour, am trying to do it on my own.

I don't think you can do it with dynamic properties. Create a regular property and update the stylesheet after the property value changes or react to events (reinstalling the style sheet again). An alternative is to use QPalette instead of style sheets.

aamer4yu
18th November 2009, 05:29
Yes. The stylesheet needs to be reapplied if you want it to reflect changes to the variable.How does QWidget:focus work then ? You dont need to apply stylesheet whenever the focus changes,, Qt does that automatically.


I don't think you can do it with dynamic properties.I guess we can. Because when I set the dynamic property through designer in the ui file, the style does get applied.
Create a regular property and update the stylesheet after the property value changes or react to events (reinstalling the style sheet again).This is something I want to avoid, since this will involve applying stylesheet on every focus changed event at the least. I was wondering if it could be automatic like QWidget:focus stylesheet. My last option would be to subclass widgets which will give me total control.
I wish there was some way to enable keypad navigation + edit focus stuff on desktop applications too.


An alternative is to use QPalette instead of style sheets.How can I use palette to draw border color ?

wysota
18th November 2009, 09:54
How does QWidget:focus work then ? You dont need to apply stylesheet whenever the focus changes,, Qt does that automatically.
It's not a dynamic property.


I guess we can.
So why are you asking? :)

I was wondering if it could be automatic like QWidget:focus stylesheet.
Only if you implement it.


How can I use palette to draw border color ?
What do you mean? Change the colour of the frame. If a particular widget has one, of course and your style supports it.

aamer4yu
18th November 2009, 11:34
What do you mean? Change the colour of the frame. If a particular widget has one, of course and your style supports it.
I mean how can we achieve the following effect with QPalette -
"border : 3px solid blue" ??

Well from the discussion can I assume the following -
For style sheets involving dynamic properties, you will need to yourself re-apply the style sheet once the dynamic property changes. :(

and since I want this border color based on dynamic property, how should I manage the stylesheet ? Because I have a single stylesheet file and applying the whole again might be heavy, isnt it ?

wysota
18th November 2009, 12:20
I mean how can we achieve the following effect with QPalette -
"border : 3px solid blue" ??
You can't. You can't ADD elements using QPalette, you may only modify colours of existing ones. If you want to add an extra frame to a widget then subclass it.


Well from the discussion can I assume the following -
For style sheets involving dynamic properties, you will need to yourself re-apply the style sheet once the dynamic property changes. :(
Yes, that's correct.


and since I want this border color based on dynamic property, how should I manage the stylesheet ? Because I have a single stylesheet file and applying the whole again might be heavy, isnt it ?

You can reapply stylesheet on the widget only if you make sure the border is set by the widget's stylesheet and not the application stylesheet.

aamer4yu
18th November 2009, 17:07
You can reapply stylesheet on the widget only if you make sure the border is set by the widget's stylesheet and not the application stylesheet.
But I was thinking of using a single style sheet on the appliction which will handle both cases.

How to achieve this -
Say I have a stylesheet that says -
QWidget[myProperty="true"] { border: 3px solid red; }
so when my dynamic property becomes true, I apply the above stylesheet. Till here fine.
Now when the dynamic property becomes false, what styleseheet to apply ?
Since the border wont be same for all widgets, it will vary from pushbuttons to line edits to combo box etc.
So what stylesheet to use so that normal border for respective widgets are drawn ?

wysota
18th November 2009, 17:35
But I was thinking of using a single style sheet on the appliction which will handle both cases.
You can't have them all. Life would be boring if everything was easy.


Now when the dynamic property becomes false, what styleseheet to apply ?
The same one.

aamer4yu
18th November 2009, 17:57
Quote:
Now when the dynamic property becomes false, what styleseheet to apply ?
The same one.
It means border needs to be specified for all widgets to be used ? How can I use the Qt default borders ?
I mean QLineEdit has different kind of border than QListWidget. So in my stylsheet if I write -
QLineEdit[myProperty="true"] { border : 3px solid blue }
QLineEdit[myProperty="false"] { border : none } // --> this will make it borderless, while I want the default Qt border for QLineEdit. What to write instead of none ?
This applies to all other widgets.

[btw thanks for patiently answering my queries till now :)]

wysota
18th November 2009, 18:02
When you start using stylesheets, you can forget about default decorations, especially on Windows and Mac. I'm not sure what you are trying to obtain but if you just want to mark a widget that has a property set, apply an event filter on your application object and whenever you receive a paint event, draw the widget as usual but also draw a border around the widget like Designer does with layouts,

aamer4yu
18th November 2009, 18:26
I'm not sure what you are trying to obtain but if you just want to mark a widget that has a property set, apply an event filter on your application object and whenever you receive a paint event, draw the widget as usual but also draw a border around the widget like Designer does with layouts,
How to draw the border ? We will receive the paint event in eventFilter, then where to draw the border ? we cant draw outside widgets. Can you show a psuedo code ?