简体   繁体   中英

how to change the color properties of QButtonGroup in PyQt5?

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

QButtonGroup.setStyleSheet("""
                              QButtonGroup 
                                { 
                                        background-color: rgb(255, 255,255);  
                                }
                                """
                                )

But there is no such a function. I would appreciate if anyone can help (either in Python or in C++ )

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM