#include "tooltipbutton.h" #include #include #include TooltipButton::TooltipButton(QWidget *parent) : QLabel(parent) { setMouseTracking(true); _tooltip_parent_widget = this; _tooltip_label = new QLabel(this); _tooltip_label->setText("Tooltip."); _tooltip_label->setMaximumWidth(360); _tooltip_label->setWordWrap(true); _tooltip_label->setStyleSheet("padding: 8px; background-color: #222; border: 1px solid #fff; border-radius: 5px; color: #fff"); _tooltip_label->hide(); _tooltip_label_arrow = new QWidget(this); _tooltip_label_arrow->setStyleSheet("border-image: url(:/buttons/tooltip_triangle.png);"); _tooltip_label_arrow->resize(14, 7); _tooltip_label_arrow->hide(); _tooltip_opacity_effect = new QGraphicsOpacityEffect(this); _tooltip_arrow_opacity_effect = new QGraphicsOpacityEffect(this); _tooltip_label->setGraphicsEffect(_tooltip_opacity_effect); _tooltip_label_arrow->setGraphicsEffect(_tooltip_arrow_opacity_effect); } TooltipButton::~TooltipButton() { _tooltip_label->deleteLater(); _tooltip_label_arrow->deleteLater(); _tooltip_opacity_effect->deleteLater(); _tooltip_arrow_opacity_effect->deleteLater(); } void TooltipButton::enterEvent(QEvent*) { QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity"); animation->setDuration(300); animation->setStartValue(0); animation->setEndValue(1); animation->setEasingCurve(QEasingCurve::InBack); animation->start(QPropertyAnimation::DeleteWhenStopped); _tooltip_label->show(); QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity"); arrow_animation->setDuration(300); arrow_animation->setStartValue(0); arrow_animation->setEndValue(1); arrow_animation->setEasingCurve(QEasingCurve::InBack); arrow_animation->start(QPropertyAnimation::DeleteWhenStopped); _tooltip_label_arrow->show(); } void TooltipButton::leaveEvent(QEvent*) { QPropertyAnimation *animation = new QPropertyAnimation(_tooltip_opacity_effect, "opacity"); animation->setDuration(300); animation->setStartValue(1); animation->setEndValue(0); animation->setEasingCurve(QEasingCurve::InBack); animation->start(QPropertyAnimation::DeleteWhenStopped); connect(animation, &QPropertyAnimation::finished, _tooltip_label, &QLabel::hide); QPropertyAnimation *arrow_animation = new QPropertyAnimation(_tooltip_arrow_opacity_effect, "opacity"); arrow_animation->setDuration(300); arrow_animation->setStartValue(1); arrow_animation->setEndValue(0); arrow_animation->setEasingCurve(QEasingCurve::InBack); arrow_animation->start(QPropertyAnimation::DeleteWhenStopped); connect(arrow_animation, &QPropertyAnimation::finished, _tooltip_label_arrow, &QLabel::hide); } void TooltipButton::moveEvent(QMoveEvent*) { updateTooltipPosition(); } void TooltipButton::resizeEvent(QResizeEvent*) { updateTooltipPosition(); } void TooltipButton::setTooltipParentWidget(QWidget *widget) { _tooltip_parent_widget = widget; _tooltip_label->setParent(widget); _tooltip_label_arrow->setParent(widget); updateTooltipPosition(); } void TooltipButton::setTooltipText(QString text) { _tooltip_label->setText(text); updateTooltipPosition(); } void TooltipButton::updateTooltipPosition() { QPoint pos_global = mapToGlobal((rect().topLeft() + rect().topRight()) / 2); QPoint pos_relative_to_tooltip_parent = _tooltip_parent_widget->mapFromGlobal(pos_global); pos_relative_to_tooltip_parent.rx() -= _tooltip_label->sizeHint().width() / 2; pos_relative_to_tooltip_parent.ry() -= _tooltip_label->sizeHint().height() + 6; // 10 for tooltip arrow _tooltip_label->move(pos_relative_to_tooltip_parent); QPoint tooltip_arrow_pos = _tooltip_parent_widget->mapFromGlobal(pos_global) - QPoint(7, 7); _tooltip_label_arrow->move(tooltip_arrow_pos); }