How to correctly use QProxyStyles?



  • I'm trying to hack around the issue that Qt doesn't have native support for text <-> icon spacing on QPushButtons and I can't get my approach using a QProxyStyle to work. I made a class inheriting QProxyStyle which draws a QPushButtonLabel with custom instead of hardcoded spacing and then just try to call setStyle with my custom style on a QPushButton. It doesn't work however, my custom style's drawControl is never called.
    It seems like there is a style hierarchy issue because my button is part of a UI file which inherits the default OS style already. Once QCommonStyle::drawControl calls proxy(), it returns the default QWindowsStyle with my custom style as base style and I think it should be the other way around. This way, QWindowsStyle still does all the drawing and my custom style is never reached.
    I know I can set a base style on my proxy style but since it takes ownership of the base style passed, I don't think I can specifiy the default windows style as base because it will be deleted when my proxy style goes out of scope. Any ideas?


  • Moderators

    @Justin-Sayne said in How to correctly use QProxyStyles?:

    I know I can set a base style on my proxy style but since it takes ownership of the base style passed, I don't think I can specifiy the default windows style as base because it will be deleted when my proxy style goes out of scope.

    why should it go out of scope?

    widget->setStyle( new MyProxyStyle(widget->style()) );
    

    But anyway, simply setting the proxy style wihtout a base-style should do what you want already.
    Show your code please.

    Also note that a QProxyStyle wont work properly when used in conjunction with a QStylesheetStyle (anywhere in the parent-chain!)



  • @raven-worx said in How to correctly use QProxyStyles?:

    why should it go out of scope?

    widget->setStyle( new MyProxyStyle(widget->style()) );
    

    The QWidget doc says, setStyle doesn't take ownership of the QStyle pointer passed, so it seemed like passing it this way produces memory leaks. That's also why I had it coupled to the lifetime of my dialog I was showing the button in.

    Also note that a QProxyStyle wont work properly when used in conjunction with a QStylesheetStyle (anywhere in the parent-chain!)

    I can show the code if you still want, but I'm pretty sure, that's the problem. The button is part of a UI file where I'm making heavy use of style sheets. Is there any other way to get customizeable icon/text spacing for QPushButton and ideally QToolButton as well? I can't believe Qt doesn't support this natively...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.