Sunday, 15 March 2015

QT - overriding QToolButton paint event -


My own widget is MyToolButton which comes from QToolButton . In the paint event, I should draw only "background" from the original QToolButton . I mean, I just need to draw style (clicks, hover, etc.), but without text and image. I want to attract these things through my own (with custom formatting etc.) when I call on QToolButton :: paintEvent (ev) everything is painted. How to separate image and text from painting? I think I should use the drawControl () , drawPrimitive () and drawComplexControl () but can not understand all these states < / P>

The source code for zero is QToolButton :: paintEvent (QPaintEvent *) : < / P>

  QStylePainter p (this); QStyleOptionToolButton opt; InitStyleOption (& amp; op); P.drawComplexControl (QStyle :: CC_ToolButton, Opt);   

QStylePainter :: drawComplexControl call QStyle :: drawComplexControl , which is a pure virtual function, and its implementation depends on a style Is, you are using. You need to go to the source code of Concrete Surplus (like QWindowsXPStyle or QGtkStyle ), find the drawComplexControl () implementation and use it for your < Code> paintEvent . For a simple control like QPushButton , you can rewrite your drawing quite style-freely- Redrawing QToolButton independently will work a bit more difficult a good Will use the source code of the option QCommonStyle .

No comments:

Post a Comment