PyQt5, как сделать кнопку переключения изображения с QAbstractButton - PullRequest
1 голос
/ 14 марта 2019

У меня есть класс PicButton, основанный на QAbtractButton, у которого есть normal, hover и нажатый.Однако при нажатии кнопка ненадолго меняется на 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 Ответ

2 голосов
/ 14 марта 2019

QAbstractButton имеет свойство , которое можно проверить , которое позволяет вам реализовать желаемую логику:

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...