Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-131479

ToolTip style widget is moved up 20 pixels when calling show()

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.8.0
    • None
    • macOS
    • 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

        For Gerrit Dashboard: QTBUG-131479
        # Subject Branch Project Status CR V

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            con Eike Ziller
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes