Why not use native widgets?
-
I am wondering whether the more serious limitation relates to lack of optimization for repainted regions, as a stateful resource would be able to compute the bounds of a physical region having become invalid, based on changes in conceptual state, and its own understanding of how such changes relate to its own paint process.
-
@brainchild I think you overestimate that optimization potential.
Simple controls do simple things e.g. a panel will usually just fill it with a color or a gradient. If control has text, like buttons or menu item, it's ballpark the same work to calculate proper text positioning, kerning etc. as simply drawing the whole thing clipped to the region.
If you think about it the set of basic UI controls is not that large and they are all pretty basic. More complicated ones like lists, tables etc. are just composition of the basic ones. If you have a very complicated control, like a chart or 3D scene, there's no native controls for that anyway, so all painting is custom one way or another. If you have highly animated UI toolkit it probably draws using an accelerated API like OpenGL or Vulkan, and the cost of figuring out on the CPU sub-changes needed is often a pessimization, as the GPU can draw them faster than the CPU can feed it anyway, so it's actually more performant to just draw it all.
I don't think many toolkits do the sub-control optimizations that you think of.
-
@Chris-Kawa said in Why not use native widgets?:
I don't think many toolkits do the sub-control optimizations that you think of.
The design that has been familiar to me is that container widgets manage the events of their children, such that it would be important for a container to propagate up the chain the specific bounds of some region that has become invalid, as would correspond to some non-container descendant, to avoid repainting the entire container including descendants. It seems that Qt uses a different model of event propagation, with a widget hierarchy being applied more narrowly to resolve concerns such as position and z-order of paint composition, or recipient of mouse and key events.
-
Qt does exactly the same - how should it work otherwise?
The code of Qt is open-source - take a look at the event handling or make a breakpoint in your paintEvent() to see where it comes from and you will see the event propagation exactly like it's done in other frameworks.
Also Qt is using native drawing methods to draw it's widgets - see the windows style plugin on how it works. -
@brainchild It seems like you think the native window calls are more performant that Qt's. As has been explained excessively, Qt provides the faster approach by not creating individual OS objects. So, Qt is faster than the native API. You can use a more modern API for Windows for comparable native performance (I don't know any of those). However, these are usually short lived and you need to rewrite your application every couple of years. Qt brings a lot more stability which is a good thing for commercial applications. And as told repeatedly: Qt uses the OS's functionality to draw buttons, labels, combo boxes, etc. An update to the looks by the OS will then also update Qt's look. Rarely does Qt need to change to conform to a new look.
Concerning event handling: Qt translates events one-to-one from the native events to Qt events. No information is lost. There is nothing (nothing I know of) that the native API can do that Qt can't do. I think there are a few special things on macOS. However, there is always the possibility to also receive the native events if you want to. And the optimization of draw calls you have been talking about is also done by Qt. Qt will only request a redraw for the widgets within the QRect, just as the OS would do by itself. And then finally only the part of the native window will be changed with the image from the back buffer. The only "performance penalty" is the use of a back buffer instead directly drawing onto the window. This, however, is necessary for a good user experience anyway. So, the best implementation cannot get any faster than this.
Finally, the reason for using Qt is not to write a Windows program. If you want to do that, just use the native API. The reason for using Qt is writing portable code. And it is still the best tool for writing portable desktop applications. I even think it has the superior API compared to Windows. Concerning the looks of your application: We spend a great deal to have more modern look for our professional application. This means using stylesheets. Using native APIs we would not be able to do that. We specifically switched to Qt to update to a more modern look. Also, we tried to have a similar look on all platforms. For us it is a hindrance that Qt uses the native APIs for drawing controls. I would prefer to not use stylesheets but change colors solely based on the palette. This does not work because Qt uses the native APIs. I would use the Fusion style, but that is honestly too playful for a professional application. Furthermore, some stylesheet changes will break a lot. For example, if you change any part of the look of a QPushButton, layouts will compute a different width for the button. Suddenly, your 'Ok' and 'Cancel' buttons at the bottom of the dialog will have different widths instead of being the same width as expected. So, I vote for Qt to drop the native drawing calls and fake the whole look so I can use palettes instead of stylesheets and not break any layouting while changing the looks.
-
Yes, I have begun to understand better than before. I think the explanation that feels clear to me is that Qt uses native calls to draw the primitive elements with a native appearance, but provides through its own libraries many of the higher-level fundamentals, such as layout and positioning, invalidation of window regions, translation between primitive and high-level events, and so on. Thus, it employs a hybrid model, abstracting most features that port well into its own libraries, but reserving native calls for the lowest-level operations, including painting primitive widgets.
The topic is one that has seemed to be a source of much confusion, not just for me. It might be helpful if the Qt team would include some explanation in the main documentation, or perhaps a post in some blog that is discoverable through a web search.
-
@brainchild said in Why not use native widgets?:
It might be helpful if the Qt team would include some explanation in the main documentation, or perhaps a post in some blog that is discoverable through a web search.
Why? What exactly do you need? That Qt does it's own painting? Is documented. That Qt abstracts the OS? That's the main purpose for this library.
-
@Christian-Ehrlicher said in Why not use native widgets?:
@brainchild said in Why not use native widgets?:
Why? What exactly do you need? That Qt does it's own painting? Is documented. That Qt abstracts the OS? That's the main purpose for this library.
Perhaps everyone already understands except for me, but it's not my impression reading comments and questions.
-
-
@Christian-Ehrlicher said in Why not use native widgets?:
I have not found an explanation as the one discussed here in the referenced documentation, but I will review it more carefully when able.
-
The sorts of stuff you are asking about are pretty much just obscure implementation details. Most people don't make an assumption that 1:1 platform widgets to QWidgets would be a major performance improvement, so it wouldn't occur to folks that the opposite needs to have some sort of explanation or justification. It literally has zero effect on day to day usage of the Qt public API. And for most people who care about digging into the low level platform specific guts, having a caching layer in Qt makes sense by the time they get anywhere near a place where it matters.
-
@wrosecrans said in Why not use native widgets?:
The sorts of stuff you are asking about are pretty much just obscure implementation details.
Yes, but questions about the issues are asked frequently, with an abundance of misunderstanding or misinformation given in response. As long as the questions are asked, it seems better that answers are available and accurate, especially against an alternative of being available but inaccurate.