简体   繁体   中英

Trigger a paint event inside the enter event in Qt

Q: I am trying to trigger a paint event inside the enter event in Qt but I am getting an error, basically can't call the painter inside the enter mouse event. What I need is to darken the image(as button) as I hover the mouse. Is this even possible? Thank you.

from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore

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

    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.drawPixmap(event.rect(), self.pixmap)
        
        painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,0)))  
        
    def enterEvent(self, event):
        #error with the line below: 'PySide2.QtGui.QEnterEvent' object has no attribute 'rect'
        #painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,128)))  
        
        print('hovering')

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

window_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()

myPixmap = QtGui.QPixmap("image.jpg")

my_button = PicButton(myPixmap)
my_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
my_button.setMaximumSize(200,100)


vlayout_wid.addWidget(my_button)

window_wid.setLayout(vlayout_wid)
window_wid.show()

在此处输入图像描述

Ok, I solved the problem with underMouse() within the paintEvent . Not sure if it's the best solution but it's working now. Just wish I was able to brighten the image instead of darkening.

from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore

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

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

    def paintEvent(self, event):
        #pix = self.pixmap_hover if self.underMouse() else self.pixmap

        painter = QtGui.QPainter(self)
        
        painter.drawPixmap(event.rect(), self.pixmap)
        
        
        pix = painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor (0,0,0,128)))  if self.underMouse() else self.pixmap

    def enterEvent(self, event):
        self.update()
        
    def leaveEvent(self, event):
        self.update()
                
    def sizeHint(self):
        return QtCore.QSize(200, 100)

        
window_wid = QtWidgets.QWidget()
vlayout_wid = QtWidgets.QVBoxLayout()

pixmap = QtGui.QPixmap("image.jpg")

my_button = PicButton(pixmap)
my_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
#my_button.setMaximumSize(200,100)


vlayout_wid.addWidget(my_button)

window_wid.setLayout(vlayout_wid)
window_wid.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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