Article directory
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 QToolTip
implement the bubble style you need by rewriting the class.
It has been supported since Qt 5.9.9QToolTip
. Basically all QWidget
components inherited from can setToolTip()
set bubble information. The Qt base classes shown below can all set bubble information.
- setToolTip: QAction QGraphicsItem QListWidgetItem QStandardItem QSystemTrayIcon QTableWidgetItem QTextCharFormat QTreeWidgetItem QWidget QWinThumbnailToolButton
- setToolTipDuration: QWidget
- setToolTipsVisible: QMenu
QWidget
Basically all Qt control classes inherited from support the setting of bubbles. This article uses QLabel
and QPushButton
as examples to demonstrate the function of text bubbles.
the code
The code is very simple, just drag one QLabel
and 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);
}