Important: Please read the Qt Code of Conduct -

QToolTip::showText: How to get the rect right?

  • I'd like to use a tooltip as user feedback in a case where the user cannot drop something, but might have no idea why not.

    Therefore, I show a tooltip in the dragEnter event, using

        const QPoint globalPos = QCursor::pos();
        const QString text("Data cannot be changed while export is running");
        const QRect rect(QPoint(), geometry().size());
        QToolTip::showText(globalPos, text, this, rect); // Note: 'this' is a QGraphicsView

    The tooltip should ideally disappear when the drag leaves the widget. The forth parameter 'rect' of QToolTip::showText sounds like the right place to start:

    "If you specify a non-empty rect the tip will be hidden as soon as you move your cursor out of this area.

    The rect is in the coordinates of the widget you specify with w. If the rect is not empty you must specify a widget. Otherwise this argument can be 0 but it is used to determine the appropriate screen on multi-head systems." (Qt docs)

    However, the tooltip disappears at surprising places, so I assumed my rect must be wrong. I tried various things (e.g. a fixed rect of 0/0 to 200/100), but the behavior of QToolTip::showText follows no rule I can discern.

    Can somebody explain how to use the rect properly, or show an example?

  • Lifetime Qt Champion


    What version of Qt ? On what platform ?

  • Qt 4.8 on Windows, in this case.
    I've briefly looked through the reported bugs for QToolTip, but couldn't find anything that concerns the rect.

  • Lifetime Qt Champion

    Can you provide a minimal code sample to test that ?

  • Not really. I since threw out QToolTip in favor of a different user feedback approach.

    But for anyone looking into this in the future, I have two nuggets of information:

    • None of the examples (at least up to 5.6) uses the 'rect' parameter of QToolTip::showText
    • Even in Qt source itself, the rect is rarely used. One case is in qmdisubwindow.cpp. The code there obtains the rect from subControlRect. Since that method is documented to return a rect in screen coordinates, I assume that QToolTip::showText also expects a rect in screen coordinates.

Log in to reply