[Qt] Text bubble pops up when the mouse is hovering


Requirement description: When the mouse hovers over a control, display the set bubble information

In the previous version of Qt5, setting the bubble needs to be realized through other class libraries in C++, and related Qt classes have been implemented in Qt5. And in addition to setting text bubbles, you can also QToolTipimplement the bubble style you need by rewriting the class.

It has been supported since Qt 5.9.9QToolTip . Basically all QWidgetcomponents inherited from can setToolTip()set bubble information. The Qt base classes shown below can all set bubble information.

QWidgetBasically all Qt control classes inherited from support the setting of bubbles. This article uses QLabeland QPushButtonas examples to demonstrate the function of text bubbles.

the code

The code is very simple, just drag one QLabeland one on the ui interface QPushbutton, and right-click the signal slot on the ui interface to go to the slot.

#include "widget.h"
#include "ui_widget.h"
#include <QImage>
#include <QPixmap>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    
    
    ui->setupUi(this);


    QPixmap img(":/Win11.jpg");

    ui->label->setPixmap(img);
    ui->label->setScaledContents(true);
    ui->label->setToolTip(QString("壁纸《Win11.png》"));
    ui->pushButton->setToolTip(QString("隐藏或显示图片"));
}

Widget::~Widget()
{
    
    
    delete ui;
}

void Widget::on_pushButton_clicked()
{
    
    
    if(ui->label->isVisible())
        ui->label->hide();
    else
        ui->label->setVisible(true);
}

demo

insert image description here

supported SetToolTipcontrols

insert image description here

Guess you like

Origin blog.csdn.net/Fuel_Ming/article/details/124348526