Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyqt changing the color of a button when pressed using a stylesheet

i am trying to change the color of a button when is press, and look like it can be done directly with in the stylesheet with out to do a function and then connect it.

in http://qt.developpez.com/doc/4.7-snapshot/stylesheet-examples/ show this example:

QPushButton#evilButton {
    background-color: red;
    border-style: outset;
    border-width: 2px;
    border-radius: 10px;
    border-color: beige;
    font: bold 14px;
    min-width: 10em;
    padding: 6px;
}
QPushButton#evilButton:pressed {
    background-color: rgb(224, 0, 0);
    border-style: inset;
}

how can translate that to python pyqt?

like image 886
pelos Avatar asked Dec 06 '22 03:12

pelos


2 Answers

i was able to make it work like this, that way the color button is blue and when is press changes to red

    btn_text = QString("this font color")
    btn = QPushButton(btn_text)

    btn.setStyleSheet("QPushButton { background-color: blue }"
                      "QPushButton:pressed { background-color: red }" )
like image 114
pelos Avatar answered Jan 02 '23 23:01

pelos


No translation needed, it is a stylesheet: a text document used by qt (and therefore by pyqt, since pyqt calls qt). For example:

self.groupBox.setStyleSheet("""
       QGroupBox 
       { 
           background-color: rgb(255, 255,255); 
           border:1px solid rgb(255, 170, 255); 
       }
       """
)

Take a look at http://vimeo.com/50033293.

like image 42
Oliver Avatar answered Jan 03 '23 00:01

Oliver