What if the pixel happens to be transparent in this particular theme? What if the user changes the theme during the life of the program? What if...?
What if the pixel happens to be transparent in this particular theme? What if the user changes the theme during the life of the program? What if...?
I complete agree that this is not perfect, hence my use of single quotes around my use of "solved". So, if anyone has a better solution, then please feel free to share it with me.In fact, I just wish that Qt would provide us with more information about the colour scheme used to render various widgets.
Qt doesn't have anything to do with this. It's not Qt that renders the theme on Windows Vista and up. You can probably use the native API to ask for that colour if the theming API supports it. Qt reports all it can in QPalette. If the colour you are looking for is not there then all that is left is Windows theming API.
I appreciate that Qt has nothing to do with it, but all I am trying to do is to have my widget using colours that are consistent with the other widgets that are (indirectly) rendered by Qt. Also, I want this to work on Windows, Linux and Mac OS X. So, for now, the solution I have is far from perfect, but it's still better than nothing (I could however improve it to at least address the change of theme during the life of the application).
The solution you currently have will fail on Mac and some of the other desktop environments in their default or personalized configurations. Mac draws most of things with rounded corners so at best you will likely hit a transparent area. There are also widget styles that don't draw a border at all or draw a gradient border or do one of the numerous other things that will make your approach fail. Using stylesheets will also make your approach fail on pretty much every possible configuration. As I understand it, you have tested your solution on Windows 7. Did you consider other systems too? You should either assume your code to be unportable and focus on a particular plartform configuration or you should accept the fact that your solution is not a solution at all (not even a "solution") and just do the drawing using QStyle API and QPalette. Especially considering the fact that a border is really a trivial issue. Think about drawing highlights or opaque areas the theme draws as a gradient. You can't possibly emulate that without reverting to the API that originally drew those elements. The final result can be even worse than if you had used a standard approach which works on most systems (which is all excluding Vista+ and MacOSX) out of the box.
Ok, what I am after is drawing a border (or part of a border, e.g. only the top part of a border) around a widget using a colour which is consistent with those used by other widgets.
I have indeed tested my current approach on Windows 7, but also on Mac OS X (Lion) and Ubuntu (11.10), and it all works as expected. Now, I agree that there is no guarantee whatsoever that it would work in some other random settings, and I have never claimed that it would.
Regarding QStyle (and QStylePainter), I have looked into it and it would work fine for a full border (I have tried it), but what about the case where I only need part of a border?...
What if you would let the QStyle draw a frame onto a QPixmap with the same dimensions as your widget, cut of the part you don't need, and draw the pixmap as your frame? If you can't just draw the rest of your widget over the part of the border you don't need.
Last edited by ChiliPalmer; 29th March 2012 at 16:55.
Why don't you use QStylePainter::drawPrimitive() with QStyle::PE_Frame (or similar) then?
What exactly do you mean with "part of a border"? You can probably still do it for most cases as the style should report the width of a frame through QStyle API (probably as a pixel metric). If you have that size, you can introduce clipping to draw only the part you need. However I can't think of a case where you'd only want to draw one side of a frame, that wouldn't be a frame anymore, would it?Regarding QStyle (and QStylePainter), I have looked into it and it would work fine for a full border (I have tried it), but what about the case where I only need part of a border?...
Well if you consider that a border consists of a top, left, bottom and right edges, then a full border would have all four edges rendered, and yes it works fine using QStylePainter::drawPrimitive() with QStyle::PE_Frame. However, I am not sure I could use something similar just to render, say, the top edge alone (i.e. no left, bottom or right edge). Then again, I haven't tried, so who knows it might be possible...?
Ok but what for do you need just the top part of a "3D" frame?
First, an edge consists of a one-pixel wide line in my case. Otherwise, I have a QWebView-based widget which I use in a window which can either be docked or not. When it's docked, that widget doesn't look good (to me, at least), so I add a full frame around it, this on Windows and Linux while only a top edge on Mac OS X. Then, when the window isn't docked, I have only a top edge on all three platforms. So, that's it, it's purely for aesthetics.
So basically you have a one pixel wide line and not a part of a frame which is usually at least two pixel wide (see QStyle::pixelMetric for PM_DefaultFrameWidth)Why don't you just use one of QPalette's Light, Midlight, Dark, Mid or Shadow values?
Yes, a one-pixel wide line. I honestly can't remember of wide the frame was when I tried QStylePainter::drawPrimitive(). I just know that it worked for the full frame. Anyway, I did succesfully use QPalette's Light, Midlight, etc. values before, but none of them corresponds to the actual colour used by the theme to render a frame, hence my original message.![]()
Bookmarks