[PyQt] stylesheets for custom classes and properties?

Patrick Stinson patrickkidd at gmail.com
Tue Apr 11 07:45:37 BST 2017


> On Apr 10, 2017, at 11:41 PM, Florian Bruhin <me at the-compiler.org> wrote:
> 
> Hi,
> 
> On Mon, Apr 10, 2017 at 11:30:23PM -0700, Patrick Stinson wrote:
>> I looked in the docs but no dice. Are custom pyqt classes supported
>> via Qt stylesheets? Hard to think how this would be possible?
> 
> I'm not sure if matching by class works, but I usually do this in
> __init__:  self.setObjectName(self.__class__.__name__)
> 
> And then match on the object name, like here:
> https://github.com/qutebrowser/qutebrowser/blob/master/qutebrowser/mainwindow/statusbar/bar.py#L92 <https://github.com/qutebrowser/qutebrowser/blob/master/qutebrowser/mainwindow/statusbar/bar.py#L92>

Ah, a very good idea. I got custom classes to work, but had to create an intermediate subclass of QWidget to do it:

class Stylesheeter(QWidget):
    def paintEvent(self, event):
        opt = QStyleOption()
	opt.initFrom(self)
        painter = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)

> 
> Or do you mean styling a class with completely custom painting via
> stylesheets? That's not possible as far as I know, but it's not in Qt
> either.

no, just normal styling.

> 
>> What about dynamic properties? This one seems more straightforward to
>> implement.
> 
> See the same stylesheet - works with @pyqtProperty decorated properties,
> but you'll need to re-set the stylesheet after changing it.
> 
> Also see: http://pyqt.sourceforge.net/Docs/PyQt5/qt_properties.html <http://pyqt.sourceforge.net/Docs/PyQt5/qt_properties.html>

Excellent, thank you.

> 
> Florian
> 
> -- 
> http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
>   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
>         I love long mails! | http://email.is-not-s.ms/
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20170410/9e45eaf1/attachment-0001.html>


More information about the PyQt mailing list