TestTooltip::TestTooltip(const QString &message, QWidget* parent) : QWidget(parent, Qt::ToolTip) , m_msgLabel(new QLabel(message, this)) { QVBoxLayout *layout = new QVBoxLayout(); layout->addWidget(m_msgLabel); setLayout(layout); } TestTooltip::~TestTooltip() { } void TestTooltip::updateTooltip(QWidget *wid) { QPoint const pos = wid->mapToGlobal(QPoint(wid->width() / 2, 0)); QScreen* screen = QApplication::screenAt(wid->mapToGlobal(QPoint(0,0))); if (screen == nullptr) { return; } QRect scr = screen->geometry(); updateGeometry(); QSize sz = sizeHint(); move(qMax(pos.x() - 27, scr.left() + 2), pos.y() - sz.height()); } LabelWithTooltip::LabelWithTooltip(const QString &text, QWidget *parent) : QLabel(text, parent) , m_tooltip(new TestTooltip(text, nullptr)) { setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); setContentsMargins(0, 0, 0, 0); } LabelWithTooltip::~LabelWithTooltip() { delete m_tooltip; } void LabelWithTooltip::enterEvent(QEnterEvent *event) { QLabel::enterEvent(event); m_tooltip->updateTooltip(this); m_tooltip->show(); } void LabelWithTooltip::leaveEvent(QEvent *event) { QLabel::leaveEvent(event); m_tooltip->hide(); } void LabelWithTooltip::showEvent(QShowEvent *event) { QLabel::showEvent(event); // update position of the tooltip when the label appears m_tooltip->updateTooltip(this); } void LabelWithTooltip::resizeEvent(QResizeEvent *event) { QLabel::resizeEvent(event); // update position of the tooltip when the label size changes m_tooltip->updateTooltip(this); } void LabelWithTooltip::moveEvent(QMoveEvent *event) { QLabel::moveEvent(event); // update position of the tooltip when the label position changes m_tooltip->updateTooltip(this); } QSize LabelWithTooltip::sizeHint() const { return(QSize(200, 20)); }