gpt4 book ai didi

python - PyQt5,如何使用 QAbstractButton 制作图像切换按钮

转载 作者:行者123 更新时间:2023-12-01 01:07:57 25 4
gpt4 key购买 nike

我有一个基于 QAbtractButton 的 PicButton 类,它具有正常、悬停和按下的功能。然而,当单击该按钮时,仅短暂更改为 pixmap_pressed,然后切换回标准。

如何使其行为像切换按钮一样,以便按下的像素图在按下后保持不变?

import numpy as np
import time, sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QMainWindow

class PicButton(QAbstractButton):
def __init__(self, pixmap, pixmap_hover, pixmap_pressed, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_hover = pixmap_hover
self.pixmap_pressed = pixmap_pressed

self.pressed.connect(self.update)
# self.released.connect(self.update)

def paintEvent(self, event):
pix = self.pixmap_hover if self.underMouse() else self.pixmap
if self.isDown():
pix = self.pixmap_pressed
painter = QPainter(self)
painter.drawPixmap(event.rect(), pix)

def enterEvent(self, event):
self.update()

def leaveEvent(self, event):
self.update()

def sizeHint(self):
return self.pixmap.size()

class App(QMainWindow):
def __init__(self):
super().__init__()
self.left = 0
self.top = 0
self.width = 800
self.height = 800
self.initUI()

def initUI(self):
self.setGeometry(self.left, self.top, self.width, self.height)
self.recBtn = PicButton(QPixmap('./img/playrecstop/rec_512.png'),QPixmap('./img/playrecstop/recHL_512.png'),\
QPixmap('./img/playrecstop/recActive_512.png'))
self.recBtn.setText("rec")
self.recBtn.clicked.connect(self.controlButtons)

self.stopBtn = PicButton(QPixmap('./img/playrecstop/stop_512.png'), QPixmap('./img/playrecstop/stopHL_512.png'),\
QPixmap('./img/playrecstop/stopActive_512.png'))
self.stopBtn.setText("stop")
self.stopBtn.clicked.connect(self.controlButtons)

self.leftLayout = QHBoxLayout()
self.rightLayout = QHBoxLayout()

self.rightLayout.addWidget(self.recBtn)
self.rightLayout.addWidget(self.stopBtn)

self.mainLayout = QHBoxLayout()
self.mainLayout.addLayout(self.leftLayout)
self.mainLayout.addLayout(self.rightLayout)

self.setCentralWidget(QWidget(self))
self.centralWidget().setLayout(self.mainLayout)
self.show()


def controlButtons(self):
sender = self.sender()
if (sender.text() == 'stop'):
print ("Stop")
elif (sender.text() == 'rec'):
print ("REC...")


if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

谢谢

最佳答案

QAbstractButton 有一个 checkable属性允许您实现所需的逻辑:

class PicButton(QAbstractButton):
def __init__(self, pixmap, pixmap_hover, pixmap_pressed, parent=None):
super(PicButton, self).__init__(parent)
self.pixmap = pixmap
self.pixmap_hover = pixmap_hover
self.pixmap_pressed = pixmap_pressed
self.setCheckable(True)

def paintEvent(self, event):
pix = self.pixmap_hover if self.underMouse() else self.pixmap
if self.isChecked():
pix = self.pixmap_pressed
painter = QPainter(self)
painter.drawPixmap(event.rect(), pix)

def enterEvent(self, event):
self.update()

def leaveEvent(self, event):
self.update()

def sizeHint(self):
return self.pixmap.size()

关于python - PyQt5,如何使用 QAbstractButton 制作图像切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55152144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com