Event hover in pyqt5

1

I would like to know how I can generate a Hover event in the bclose button of the following code so that when the mouse is over it changes its color, for example:

This is my code:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowFlags((Qt.FramelessWindowHint))
        self.setAttribute(Qt.WA_TranslucentBackground, True )
        self.setAttribute(Qt.WA_NoSystemBackground, False)
        self.setStyleSheet("background-color:qlineargradient(spread:reflect, x1:0.209, y1:0.006, x2:0.655367, y2:0.404, stop:0 rgba(88, 93, 99, 105), stop:1 rgba(20, 25, 29, 205))")
        self.bclose = QPushButton(self)
        self.bclose.setText("X")
        self.bclose.setStyleSheet("background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));color:'white';font:10pt;border:0px;")
        self.bclose.setGeometry(551,0,50,25)

    def mousePressEvent(self,event):

        if event.button() == Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()

    def mouseMoveEvent(self,event):
        if event.buttons() == Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)


app = QApplication([])
p = Principal()
p.resize(600,400)
p.show()
app.exec_()
    
asked by Revsky01 20.03.2018 в 05:10
source

1 answer

1

Qt Style Sheet handles pseudo-states like hover, checked, pressed, etc. It is also advisable to have a design sheet so as not to overwrite too much, making it more readable.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

qss = '''
QMainWindow{
    background-color:qlineargradient(spread:reflect, x1:0.209, y1:0.006, x2:0.655367, y2:0.404, stop:0 rgba(88, 93, 99, 105), stop:1 rgba(20, 25, 29, 205))
}
QPushButton{
    background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));
    color:'white';font:10pt;border:0px;
}
QPushButton:hover{
    background-color: 'red'
}
'''

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setWindowFlags((Qt.FramelessWindowHint))
        self.setAttribute(Qt.WA_TranslucentBackground, True )
        self.setAttribute(Qt.WA_NoSystemBackground, False)
        self.bclose = QPushButton(self)
        self.bclose.setText("X")
        self.bclose.setGeometry(551,0,50,25)
        self.bclose.clicked.connect(self.close)

    def mousePressEvent(self,event):

        if event.button() == Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()

    def mouseMoveEvent(self,event):
        if event.button() == Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    app.setStyleSheet(qss)
    p = Principal()
    p.resize(600,400)
    p.show()
    sys.exit(app.exec_())

More detailed information can be found at the following links:

answered by 20.03.2018 / 08:19
source