Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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!

    QToolTip::showText(ui->txt->mapToGlobal(QPoint(200,0)),"message");
    

  • Lifetime Qt Champion

    Hi
    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