How to make QToolTip::showText anchor from bottom left?

  • I have a QMainWindow and QWidget as it's centralWidget, I want to make all my QToolTips be shown at the lower left of centralWidget, how to achieve?

  • Lifetime Qt Champion


    How are you showing the tooltip currently ?

  • @SGaist Hi, currently, when my mouse move to a position, I get the object under the mouse position, then use QToolTip::showText(pos,info), I try to get the global lower left pos of the centralWidget, but I can't get what I need.

  • Lifetime Qt Champion

    Are you using that widget geometry ?
    What are you currently getting ?

  • @SGaist my top window is QMainWindow, and central widget is QMdiArea, and QWidget under QMdiArea, so I use the following way:
    int xOffSet = 27 ;//x offset to the mainwindow
    int yOffSet = 9 ;//y offset to the mainwindow
    int mainWinHeight = this->parentWidget()->parentWidget()->parentWidget()->height();
    QPoint pos = this->parentWidget()->parentWidget()->parentWidget()->parentWidget()->mapToGlobal(QPoint(xOffSet, mainWinHeight-yOffSet ));
    QToolTip::showText(pos, QString::fromStdString(info));

    The issue is, I can get exactly what I want, I want the toolTip window always just touch the QWidget

  • @SGaist I want it always in the yellow rect:

  • Lifetime Qt Champion

    Searching back parents is a bad sign. You should rather send the tooltip content using signal and slots and let the widget you want to show the tooltip on handle the position.

  • @SGaist since I can't get the tip in the yellow region, so call parent, but still not exactly in the yellow region. the issue for me is how to get the position of the yellow region.

  • Lifetime Qt Champion

    This :
    QPoint pos = this->parentWidget()->parentWidget()->parentWidget()->parentWidget()->
    Is just a disaster waiting to happen and cannot be recommended.

    What is more fun to do is to make a small custom widget and use signals and slots.
    So in MainWindow we add a new signal

        void ShowToolTipYellow(QString text);
    and we can then use it like
    void MainWindow::on_pushButton_released()
        emit ShowToolTipYellow("HELLO");
    we also need to connect the new signal to your custom widget which we often do in MainWins constructor
        connect(this, &MainWindow::ShowToolTipYellow, ui->yellow, &YellowToolTipBox::ShowToolTip  );

    Then to the custom object

    #include <QWidget>
    #include <QPainter>
    #include <qtooltip.h>
    class YellowToolTipBox : public QWidget
        explicit YellowToolTipBox(QWidget *parent = nullptr) : QWidget(parent) {}
    public slots:
        void ShowToolTip(QString Text) {
            QPoint pos = mapToGlobal(QPoint(0, 0));
            QToolTip::showText(pos, Text);
        // not really needed but was instead of stylesheet
        virtual void paintEvent(QPaintEvent *event) override
            QPainter p(this);
            p.drawRect(0,0, width()-1, height()-1);

    Then to use our custom widget, we use the promote feature of Creator
    alt text
    We just tell it the name of the class (YellowToolTipBox) and which .h file it lives in
    Then Press Add, then press Promote.

    Then when we run

    alt text

    test project:

    Main advantages is reusability and resilient to change.
    Even if you add or remove "parents" , it still works
    alt text

    Where as the old code
    would either crash or simply dont work as expected.

  • @mrjj appreciate for your reply. maybe I didn't describe what I want clearly. see this picture.
    Standard tooltip looks fine for me, I just want to make anchor point the QToolTip always at the "red" dot. nothing else, just this. so I was asking how to make the anchor from bottom left.
    Please let me know if you have idea can help me.Example.png

  • Lifetime Qt Champion


    well if you place a YellowToolTipBox there then it would come there.

    But normally a tooltip comes at the widget that shows it so it's unclear if you means just
    for Button Dock WIdget or for all tooltips.

  • @mrjj no, I only want to set the bottom left point of QToolTip at the red dot, not using bottom dock wdiegt, bottom dock widget is not used for tool tip. and my QToolTip is used to display information of objects on the canvas. not widget's tooltip.

  • Lifetime Qt Champion

    Ok that way.
    well you could put YellowToolTipBox and it will work.
    If you put setAttribute(Qt::WA_TransparentForMouseEvents) on it
    and removed its paintEvent if would be transparent and you can paint through it
    if that is needed.

  • @mrjj haha, if we can put a widget at the location I want, why can't we put the QToolTip at the location? for native QTooltip, it's good for me, just want to adjust the location:-)

  • Lifetime Qt Champion

    ok, well that can ofcause work too via mapToGlobal if you get the right parent
    so mapToGlabal does it right.

  • @mrjj mapToGlobal can get the global position and pass it to QToolTips::showText(pos, info), but looks this will make the QToolTip's upper left point to the given pos, right? what I want is make the lower left point of QToolTips always at the red point. I am not sure you get my point.

  • Lifetime Qt Champion


    Im not 100% sure how you want to align it but tooltip it self also add some offset as seen with Yellow as its at 0,0 but is show a little down and to the left.

    But yes the tooltip is shown with the point given as base. and then add some offset itself.

Log in to reply