Important: Please read the Qt Code of Conduct -

How to center qtooltip relative to a control?

  • This way, when the length of the prompt message changes, it cannot be centered relative to a control. Is there any other way?thanks!


  • Lifetime Qt Champion

    You can do something like this

    void MainWindow::on_pushButton_pressed()
        QString  msg = "message that is longer";
        QFontMetrics inf(QToolTip::font());
        int msglen = inf.boundingRect(msg).width();
        QToolTip::showText(ui->pushButton->mapToGlobal(QPoint( (ui->pushButton->width() / 2) - (msglen / 2),0)), msg );

    alt text

Log in to reply