简体   繁体   English

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

[英]PyQt5, how to make image toggle button with QAbstractButton

I have a PicButton class based on QAbtractButton that has the normal, hover and pressed. 我有一个基于QAbtractButton的PicButton类,具有正常,悬停和按下状态。 However, when clicked the button only changed to the pixmap_pressed briefly and then switch back to standard. 但是,单击时,按钮仅短暂更改为pixmap_pressed,然后切换回标准状态。

How can I make it behaves like a toggle button so that the pressed pixmap will stay after 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_()) 

Thanks 谢谢

QAbstractButton has a checkable property that allows you to implement the logic you want: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM