How can I make Gt开发者_高级运维kBtton round in shape?
The accepted answer said:
There is no roundness option in the base set.
Well, there is now! See the GTKWidget
documentation:
In special cases, buttons can be made round by adding the .circular style class.
So, for example:
gtk_style_context_add_class (gtk_widget_get_style_context (button),
"circular");
This works using both of the standard themes, Adwaita and HighContrast.
In GTK4, there are convenience functions to avoid having to get the StyleContext
yourself:
gtk_widget_add_css_class (button, "circular");
The above assumebutton
is a GtkWidget*
. If, instead, it's a GtkButton*
or subclass, just substitute in GTK_WIDGET (button)
of course.
There are a few options.
- Tweak the style. There is no roundness option in the base set. Some theme engines may provide border image specification or roundness but it will not work everywhere.
- Force a theme that looks the way you want it too. This is not too hard you just load a gtkrc string that specifies the theme you want. You will need to ensure users have the theme or are ok with the fallback.
- Subclass the button and draw it yourself. One way is to set border and relief to 0, pack an image, and respond to state change events to use appropriate pre-light (hover) images. Another way is to rewrite the expose event and draw the button as you wish.
精彩评论