[PyQt] Properties in PyQt5

Florian Bruhin me at the-compiler.org
Wed Sep 24 05:37:58 BST 2014


* Glenn Ramsey <gr at componic.co.nz> [2014-09-24 16:03:30 +1200]:
> Hi,
> 
> I have a dialog that has been created in Qt Designer and I would like to
> be able to set a red border on a QLineEdit based on its contents (or lack thereof).
> 
> Below is what I have tried, based on the PyQt4 version in Mark Summerfield's
> book "Rapid GUI programming with Python and Qt", but it doesn't work.

It'd be useful if you said which page/snippet so I could take a look
at that as well ;)

> What have I done wrong here?

AFAIK you need to call setStyleSheet again after a property changed.
I do something like this (from memory, untested):


class MyWidget(QWidget):

    STYLESHEET = """
        QWidget#MyWidget[error="true"] {
            color: red;
        }

        QWidget#MyWidget[error="false"] {
            color: green;
        }
    """
    _error = False

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setStyleSheet(self.STYLESHEET)

    @pyqtProperty(bool):
    def error(self):
        return self._error

    @error.setter
    def error(self, val):
        if self._error == val:
            # Updating the stylesheet is rather expensive so we want
            # to avoid it if it's unnecessary
            return
        self._error = val
        self.setStyleSheet(self.STYLESHEET)

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
             GPG 0xFD55A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140924/b729c7ad/attachment.sig>


More information about the PyQt mailing list