Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
6.8.0
-
None
-
-
767b41f80 (15.0)
Description
The following code snippet shows a widget with Qt::ToolTip in a ToolTip event. The widget has a layout and multiple subwidgets. For some reason, and only on macOS, calling show() on that widget moves it 20 pixels up. This doesn't happen on other platforms, and it doesn't happen if the widget either doesn't have multiple subwidgets, or the widget's layout hasn't SetFixedSize, or if adjustSize() isn't called on the widget.
// Copyright (C) 2024 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include <QApplication> #include <QHelpEvent> #include <QLabel> #include <QVBoxLayout> #include <QWidget> class MyWidget : public QWidget { public: bool event(QEvent *e) override { if (e->type() == QEvent::ToolTip) { if (m_tip) { delete m_tip; } m_tip = new QWidget(this); m_tip->setWindowFlags(Qt::ToolTip); auto layout = new QVBoxLayout; layout->setContentsMargins(0, 0, 0, 0); m_tip->setLayout(layout); layout->addWidget(new QLabel("foo")); // The following three lines lead to the call of show() moving the widget // 20 pixels up. // Commenting any of these lines avoids the up-movement behavior of show(). layout->addWidget(new QLabel("bar")); layout->setSizeConstraint(QLayout::SetFixedSize); m_tip->adjustSize(); m_tip->move(static_cast<QHelpEvent *>(e)->globalPos()); qDebug() << "before:" << m_tip->pos(); m_tip->show(); qDebug() << "after:" << m_tip->pos(); return true; } return QWidget::event(e); } QWidget *m_tip = nullptr; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWidget w; w.show(); return a.exec(); }
This is code that we use in Qt Creator for showing more complex tool tips (complex layout, possibly interactive).
Attachments
Gerrit Reviews
For Gerrit Dashboard: QTBUG-131479 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
606124,5 | Tooltips: Fix widget tool tip positioning on macOS | 15.0 | qt-creator/qt-creator | Status: MERGED | +2 | 0 |