PDA

View Full Version : changeEvent propagation, custom style changes



mortoray
26th October 2010, 13:49
How can I emit the changeEvent QEvent::StyleChange?

I have a style class which includes extended style information for my application. This has nothing to do with the QStyle class itself, but contains information related to layout and appearance.

What I want to do is make use of the "changeEvent" and send a StyleChange event when I modify this custom class. I see this as clearer than having a separate event for my custom style items. In most cases the widgets have to rebuild/paint themselves regardless of which style changed, so it makes sense.

The documentation on changeEvent is rather sparse however. How can I propagate this message to all windows/widgets in the application?

high_flyer
26th October 2010, 14:51
Did you read the QEvent documentation?

In general, events come from the underlying window system (spontaneous() returns true), but it is also possible to manually send events using QCoreApplication::sendEvent() and QCoreApplication::postEvent() (spontaneous() returns false).

mortoray
26th October 2010, 15:07
Yes, I even indicated I read the documentation. "sendEvent" and "postEvent" require a specific receiver. Somehow normal changeEvents are propagated to all widgets in the system. That's the mechanism I'd like to use.

high_flyer
26th October 2010, 15:46
AFAIK, no events are sent to all Widgets / Objects, rather, QApplication decides which events should go to which targets.(but I might be wrong with that)
Some, are implicit, since they will be propagated between parents and children.
If you want to send your event to all widgets in your application you could iterate through all your widgets using QApplication::allWidgets() or maybe QApplication::topLevelWidgets(). and put them as receiver for your postEvent()/sendEvent().

mortoray
26th October 2010, 16:18
I want to do whatever QT does when the style changes. However that propagates I'd like to do the same thing. Is there a function to do that? I could iterate over the top-level widgets, but I don't know how/if that event is forwarded.

mortoray
26th October 2010, 21:16
I looked at the source code and it appears on style changes QApplication simply iterates over allWidgets and sends the event directly, then calls an update on the widget. It skips items without the QA_SetStyle attribute.

It's too bad they don't expose just this functionality, that'd make my code more future proof... I suppose I could just try setStyle( style() ) and hope it does it correctly.

Added after 27 minutes:

setStyle( style() ) doesn't work, since the style hasn't changed it does nothing. I guess I'll have to create my own loop.

high_flyer
27th October 2010, 13:03
setStyle( style() ) doesn't work, since the style hasn't changed it does nothing. I guess I'll have to create my own loop.
Or override setStyle() to flag it the way you want, I think this would be simpler.