-
Notifications
You must be signed in to change notification settings - Fork 3
/
Hover.py
83 lines (63 loc) · 2.39 KB
/
Hover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
from PyQt5.QtWidgets import QApplication, QFrame, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt, QPoint, pyqtSlot
class HoverWidget(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 300, 150)
self.setWindowTitle('Hover Detail Widget')
self.layout = QVBoxLayout(self)
self.mouse_tracking_zone = QLabel(self)
self.mouse_tracking_zone.setMouseTracking(True) # Enable mouse tracking for the image label
self.layout.addWidget(self.mouse_tracking_zone)
self.setStyleSheet('''
QWidget {
background-color : #FFF;
}
QLabel {
background-color : #EDF0F6;
border-radius : 15px;
border : 0px;
}''')
self.card_widget = CardWidget(self)
self.card_widget.hide()
self.setLayout(self.layout)
self.setMouseTracking(True) # Enable mouse tracking for hover events
def mouseMoveEvent(self, event):
x_global, y_global = event.globalX(), event.globalY()
pos_relative_to_image = self.mouse_tracking_zone.mapFromGlobal(QPoint(x_global, y_global))
x_relative, y_relative = pos_relative_to_image.x(), pos_relative_to_image.y()
self.card_widget.show_card(x_relative + 20, y_relative + 15)
class CardWidget(QFrame):
def __init__(self, parent=None):
super().__init__(parent)
self.setFixedSize(150, 50)
self.setAttribute(Qt.WA_TransparentForMouseEvents)
self.label = QLabel(self)
self.label.setText('What is this?')
self.label.setAlignment(Qt.AlignCenter)
layout = QVBoxLayout(self)
layout.addWidget(self.label)
self.setStyleSheet('''
QFrame {
background-color : transparent;
}
QLabel {
border-radius : 15px;
border-top-left-radius : 0px;
border : 0px;
background-color : #FFF;
color : #4A535F;
font : 63 10pt "Montserrat Medium";
}''')
def set_text(self, text):
self.label.setText(text)
@pyqtSlot(int, int)
def show_card(self, x:int, y:int):
self.move(x, y)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
hover_widget = HoverWidget()
hover_widget.show()
sys.exit(app.exec_())