Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set and show dynamic tooltips in QT 5.12.4 LTS using C++



  • Im developing a quotegenerator for my employer that is linked to a mysql database. My boss has decided that he wants to be able to hover over check boxes and radio button and have the program show the price in a tooltip directly from the database. Since there is also a chance that these prices will be updated it is important that the tooltips reflect that updated data. Also, Im trying to avoid going and programming every single check box and radio button individually. Im attaching my code that I have so far, I cant figure out at the moment why it isnt working.

    QSqlQuery query;
    query.exec("TRUNCATE TABLE quoteTableTemp");
    
    //Here we need to iterate list of widgets and then assign the pricing tooltip
    
    
    for(int k = 0; k<listOfWidgets.count(); k++){
        qDebug() << "Looking at " << listOfWidgets.at(k)->objectName();
        query.exec("SELECT * FROM quoteTable where name = '"+listOfWidgets.at(k)->objectName()+"'");
        query.last();
        QString connectionName = query.value(2).toString();
        qDebug() <<"The connection name is " << connectionName;
    
        query.exec("SELECT * FROM quoteItems WHERE name = '"+connectionName+"'");
        query.last();
        int price = query.value(2).toInt();
        qDebug() << "the price is " << price;
        listOfWidgets.at(k)->setToolTip("$"+QString::number(price));
    
    
        qDebug() << "The real name is " << listOfWidgets.at(k)->objectName();
    
    
        if(MainWindow::findChild<QCheckBox *>("drivePulleyCheckBox")){
            qDebug() << "set checkbox tooltip";
           MainWindow::findChild<QCheckBox *>("drivePulleyCheckBox")->setToolTip("$"+QString::number(price));
        }
        else if(MainWindow::findChild<QRadioButton *>(listOfWidgets.at(k)->objectName())){
            qDebug() << "set radiobutton tooltip";
           MainWindow::findChild<QRadioButton *>(listOfWidgets.at(k)->objectName())->setToolTip("$"+QString::number(price));
        }
        qDebug() << "setting tooltips";
    

  • Qt Champions 2019

    @brochiha said in How to set and show dynamic tooltips in QT 5.12.4 LTS using C++:

    I cant figure out at the moment why it isnt working.

    What exactly does not work?



  • This post is deleted!


  • @Christian-Ehrlicher The tooltips do not show when I hover the mouse over them. My apologies for being unclear, I can see in the debug app output that it assigns the correct value to each check box or radio button but it does not show when hovered on with mouse.


  • Qt Champions 2019

    So when you set the tool tip during startup it works? I would create a small function which is called e.g. when a button is pressed which sets the tooltip to something else and see if it works then.



  • @Christian-Ehrlicher it works if the check box or radio button is already checked, however I want it to set on startup so I can hover over them without them needing to be checked. IF that makes sense. I apologize as i am a but of a noob with regards to QT and C++


  • Qt Champions 2019

    So you should start with a simple project - when I created a simple ui, put a checkbox in it and set the tooltip in the designer it is shown in the preview no matter if the checkbox is checked or not (nor if it has focus or not)



  • @Christian-Ehrlicher I can set the tooltip in the Ui but it does not update with the mysql database that I have. which is where the problem is, hence the reason for the code I have written above. from everything I have done this SHOULD cause the tooltips to show however there has to be something that I am missing. The application output with qdebug shows that the tooltip is set it is just not displayed in the ui


  • Qt Champions 2019

    Setting a tooltip during runtime works fine for me:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        int i = 0;
        QPushButton pb("Push me");
        QObject::connect(&pb, &QPushButton::clicked, &pb, [&pb, &i]()
        {
            pb.setToolTip(QString("id is: %1").arg(i++));
        });
        pb.show();
        return app.exec();
    }
    


  • @brochiha
    If you are saying that you do see the qDebug() << "set ... tooltip"; lines producing output, then thought would be that the rather nasty findChild<> statements you are using are not addressing the widgets you think they are, so you don't see the tooltip where you expect? Or, something else is later clearing the tooltip? Whether there is a database involved or not is not relevant to the fact that setToolTip() does set a tooltip correctly, and you can follow the flow of your code via a debugger or with qDebug() statements.


Log in to reply