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

2024-04-25 09:57:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基于QAbtractButton的PicButton类,它有normal、hover和pressed。但是,当点击按钮时,只会变为pixmap,然后短暂按下,然后切换回标准。你知道吗

我怎样才能使它的行为像一个切换按钮,使按下的pixmap将停留在按下后?你知道吗

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

谢谢


Tags: fromimportselfimgpnginitdefconnect
1条回答
网友
1楼 · 发布于 2024-04-25 09:57:52

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

相关问题 更多 >