PyQt5,如何使图像切换按钮与QAbstractButton

时间:2019-03-13 22:31:27

标签: python pyqt pyqt5

我有一个基于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_()) 

谢谢

1 个答案:

答案 0 :(得分:2)

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