How to change QPushButton text and background color

前端 未结 3 490
甜味超标
甜味超标 2020-12-06 10:07

I am using following code to connect QMenu to QPushButton. When button is clicked a pull-down menu with multiple sub-menu\'s items is shown.

<
3条回答
  •  囚心锁ツ
    2020-12-06 10:37

    Apart from some inconsistencies with your code example setting the background color and text color of a QPushButton works just fine with:

    setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')
    

    Example (using PySide):

    from PySide import QtGui
    
    app = QtGui.QApplication([])
    
    button = QtGui.QPushButton()
    button.setStyleSheet('QPushButton {background-color: #A3C1DA; color: red;}')
    button.setText('Press Me')
    menu = QtGui.QMenu()
    menuItem1 = menu.addAction('Menu Item1')
    menuItem2 = menu.addAction('Menu Item2')
    
    button.setMenu(menu)
    button.show()
    
    app.exec_()
    

    results in:

    enter image description here

提交回复
热议问题