how to change the color properties of QButtonGroup in PyQt5?

前端 未结 1 567
栀梦
栀梦 2021-01-27 18:38

I want to change the color of the button of a button group object in PyQt5. I tried

QButtonGroup.setStyleSheet(\"\"\"
                              QButtonGroup         


        
相关标签:
1条回答
  • 2021-01-27 19:07

    According to Qt doc:

    QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

    Therefore, you cannot set a stylesheet to it. Maybe you want a QGroupBox? Here is an example:

    import sys
    import PyQt5.QtWidgets as QtWidgets
    
    def window():
        app = QtWidgets.QApplication(sys.argv)
        w = QtWidgets.QWidget()
        w.setWindowTitle('Hello')
        w.setGeometry(100,100,200,100)
        g = QtWidgets.QGroupBox(w)
        layout = QtWidgets.QVBoxLayout()
        b = QtWidgets.QPushButton(w)
        b.setText("Hello World!")
        b1 = QtWidgets.QPushButton(w)
        b1.setText("Hello SO!")
        layout.addWidget(b)
        layout.addWidget(b1)
        g.setLayout(layout)
        w.setStyleSheet("""
            QGroupBox { background-color: rgb(255, 255,255);
            } """)
        w.show()
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        window()
    
    0 讨论(0)
提交回复
热议问题