self.setStyleSheet( """ QPushButton { background-color: #A3C1DA; color: black; font-weight: bold; border-radius: 5px; border: 2px solid #555555; padding: 5px; } QPushButton:hover { background-color: #C9E2F3; color: #555555; } """ ) 鼠标悬停在按钮上时,背景颜色未改为淡蓝色
时间: 2024-01-09 22:04:21 浏览: 180
这段代码看起来没有问题,可能是其他部分的代码影响了它的效果。你可以尝试在按钮悬停时,手动设置背景颜色,看看能否达到预期效果。例如:
```
button.setStyleSheet("background-color: #C9E2F3;")
```
如果手动设置可以生效,那么可能是其他部分的代码影响了它的效果。你可以检查一下其他修改了按钮样式的地方,或者使用调试工具查看是否有样式被覆盖。
相关问题
button_layout = QGridLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button1.clicked.connect(self.show_query1_result) button_layout.addWidget(button1, 0, 0) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button2.clicked.connect(self.show_query2_result) button_layout.addWidget(button2, 0, 1) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button3.clicked.connect(self.show_query3_result) button_layout.addWidget(button3, 1, 0) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button4.clicked.connect(self.show_query4_result) button_layout.addWidget(button4, 1, 1) button5 = QPushButton('按员工ID查询员工工资') button5.setFixedSize(200, 50) button5.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button5.clicked.connect(self.show_query5_result) button_layout.addWidget(button5, 2, 0) button6 = QPushButton('按员工ID查询鸡爪领取记录') button6.setFixedSize(200, 50) button6.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button6.clicked.connect(self.show_query6_result) button_layout.addWidget(button6, 2, 1) button_layout.setColumnStretch(2, 1) # 添加空白列来填充布局 v_layout = QVBoxLayout() v_layout.addStretch() # 添加弹性空间,使按钮布局垂直居中 v_layout.addLayout(button_layout) v_layout.addStretch(),其中self.setStyleSheet( """ QPushButton { background-color: #A3C1DA; color: black; font-weight: bold; border-radius: 5px; border: 2px solid #555555; padding: 5px; } QPushButton:hover { background-color: #C9E2F3; color: #555555; } """ )该加在什么地方
这段代码是用来设置按钮的样式,应该在创建按钮之后、添加到布局之前进行设置。可以放在其他初始化操作的代码块中,比如在__init__函数中设置。例如:
```python
class MyWidget(QWidget):
def __init__(self):
super().__init__()
# 初始化其他控件
# 设置按钮样式
self.setStyleSheet(
"""
QPushButton {
background-color: #A3C1DA;
color: black;
font-weight: bold;
border-radius: 5px;
border: 2px solid #555555;
padding: 5px;
}
QPushButton:hover {
background-color: #C9E2F3;
color: #555555;
}
"""
)
# 将按钮添加到布局中
button_layout = QGridLayout()
button1 = QPushButton('当日员工工资')
# 设置按钮大小和样式
button1.setFixedSize(200, 50)
button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button1.clicked.connect(self.show_query1_result)
button_layout.addWidget(button1, 0, 0)
# 添加其他按钮到布局
v_layout = QVBoxLayout()
v_layout.addStretch()
v_layout.addLayout(button_layout)
v_layout.addStretch()
self.setLayout(v_layout)
```
button_layout = QVBoxLayout() button1 = QPushButton('当日员工工资') button1.setFixedSize(200, 50) button1.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button1.clicked.connect(self.show_query1_result) button2 = QPushButton('当日鸡爪领取记录') button2.setFixedSize(200, 50) button2.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button2.clicked.connect(self.show_query2_result) button3 = QPushButton('查询历史员工工资') button3.setFixedSize(200, 50) button3.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button3.clicked.connect(self.show_query3_result) button4 = QPushButton('查询历史鸡爪领取记录') button4.setFixedSize(200, 50) button4.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}') button4.clicked.connect(self.show_query4_result)再添加一个退出程序的按钮
可以在这段代码后面添加以下内容来实现退出程序的按钮:
```
button5 = QPushButton('退出程序')
button5.setFixedSize(200, 50)
button5.setStyleSheet('QPushButton {background-color: #A3C1DA; color: black; font-weight: bold;}')
button5.clicked.connect(self.close)
button_layout.addWidget(button1)
button_layout.addWidget(button2)
button_layout.addWidget(button3)
button_layout.addWidget(button4)
button_layout.addWidget(button5)
main_layout.addLayout(button_layout)
```
这样就可以在界面上添加一个“退出程序”的按钮了,点击该按钮时程序会自动关闭。
阅读全文