提交 904d3c0a 编写于 作者: 之一Yo's avatar 之一Yo

提升工具提示为顶层窗口

上级 8007717b
......@@ -14,7 +14,7 @@ class Demo(QWidget):
self.button1 = QPushButton('キラキラ', self)
self.button2 = QPushButton('食べた愛', self)
self._toolTip = ToolTip(parent=self)
# self._tooltip.setDarkTheme(True)
# self._toolTip.setDarkTheme(True)
self.button1.setToolTip('aiko - キラキラ ✨')
self.button2.setToolTip('aiko - 食べた愛 🥰')
......@@ -43,16 +43,7 @@ class Demo(QWidget):
if e.type() == QEvent.Enter:
tip.setText(obj.toolTip())
tip.setDuration(obj.toolTipDuration())
pos = obj.mapTo(self, QPoint(0, 0))
x = pos.x() + obj.width()//2 - tip.width()//2
y = pos.y() - 5 - tip.height()
# adjust postion to prevent tooltips from appearing outside the window
x = min(max(5, x), self.width() - tip.width() - 5)
y = min(max(5, y), self.height() - tip.height() - 5)
tip.move(x, y)
tip.adjustPos(obj.mapToGlobal(QPoint()), obj.size())
tip.show()
elif e.type() == QEvent.Leave:
tip.hide()
......
ToolTip[dark="false"] {
ToolTip[dark=false]>QFrame {
border: 1px solid rgba(0, 0, 0, 0.06);
border-radius: 5px;
background-color: rgb(243, 243, 243);
}
ToolTip[dark="true"] {
ToolTip[dark=true]>QFrame {
border: 1px solid rgb(28, 28, 28);
border-radius: 5px;
background-color: rgb(43, 43, 43);
......@@ -15,10 +15,10 @@ QLabel {
font: 15px 'Segoe UI', 'Microsoft YaHei';
}
QLabel[dark="false"] {
QLabel[dark=false] {
color: black;
}
QLabel[dark="true"] {
QLabel[dark=true] {
color: white;
}
\ No newline at end of file
# coding:utf-8
from PyQt5.QtCore import QFile, QPropertyAnimation, QTimer, Qt
from PyQt5.QtCore import QFile, QPropertyAnimation, QTimer, Qt, QPoint, QSize
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import (QApplication, QFrame, QGraphicsDropShadowEffect,
QHBoxLayout, QLabel)
......@@ -11,27 +11,33 @@ class ToolTip(QFrame):
super().__init__(parent=parent)
self.__text = text
self.__duration = 1000
self.container = QFrame(self)
self.timer = QTimer(self)
self.hBox = QHBoxLayout(self)
self.setLayout(QHBoxLayout())
self.containerLayout = QHBoxLayout(self.container)
self.label = QLabel(text, self)
self.ani = QPropertyAnimation(self, b'windowOpacity', self)
# set layout
self.hBox.addWidget(self.label)
self.hBox.setContentsMargins(10, 7, 10, 7)
self.layout().setContentsMargins(15, 10, 15, 15)
self.layout().addWidget(self.container)
self.containerLayout.addWidget(self.label)
self.containerLayout.setContentsMargins(10, 7, 10, 7)
# add shadow
self.shadowEffect = QGraphicsDropShadowEffect(self)
self.shadowEffect.setBlurRadius(32)
self.shadowEffect.setBlurRadius(25)
self.shadowEffect.setColor(QColor(0, 0, 0, 60))
self.shadowEffect.setOffset(0, 5)
self.setGraphicsEffect(self.shadowEffect)
self.container.setGraphicsEffect(self.shadowEffect)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.hide)
# set style
self.setAttribute(Qt.WA_StyledBackground)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setWindowFlags(Qt.Tool | Qt.FramelessWindowHint)
self.setDarkTheme(False)
self.__setQss()
......@@ -64,7 +70,6 @@ class ToolTip(QFrame):
def setDarkTheme(self, dark=False):
""" set dark theme """
dark = 'true' if dark else 'false'
self.setProperty('dark', dark)
self.label.setProperty('dark', dark)
self.setStyle(QApplication.style())
......@@ -76,4 +81,25 @@ class ToolTip(QFrame):
def hideEvent(self, e):
self.timer.stop()
super().hideEvent(e)
\ No newline at end of file
super().hideEvent(e)
def adjustPos(self, pos: QPoint, size: QSize):
""" adjust the position of tooltip relative to widget
Parameters
----------
pos: QPoint
global position of widget
size: QSize
size of widget
"""
x = pos.x() + size.width()//2 - self.width()//2
y = pos.y() - self.height()
# adjust postion to prevent tooltips from appearing outside the screen
desk = QApplication.desktop()
x = min(max(0, x), desk.width() - self.width() - 5)
y = min(max(0, y), desk.height() - self.height() - 5)
self.move(x, y)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册