Qt5制作鼠標懸停顯示Hint的ToolTip


在日常生活中的使用的軟件中,我們經常會遇到這樣的情況。 
我們在網頁上,有些網頁鏈接的文字(比如文章標題,知乎問題標題,百度的詞條等)因為太長了,而顯示不出來,但是鼠標懸停在上面的時候就可以顯示出來。 
我們在QQ上或者某些輸入框內,我們不知道應該輸入什么內容,但是鼠標如果懸停在輸入框內的時候,會產生一個友好信息的hint。 
實現方法,就是我們今天的ToolTip設置。

代碼如下: 
ItemWidget.h

#ifndef ITEMWIDGET_H #define ITEMWIDGET_H #include <QWidget> #include <QLabel> #include <QPushButton> #include <QVBoxLayout> #include <QHBoxLayout> //class CLabel; class ItemWidget : public QWidget { Q_OBJECT public: explicit ItemWidget(QWidget *parent = 0); void setText(QPixmap pixmap, QString name, QString info); void setText(QString info); signals: public slots: private: QLabel *labelIcon; QLabel *labelName; QLabel *labelInfo; QHBoxLayout *horLayout; QVBoxLayout *verlayout; protected: bool event(QEvent *e); }; #endif // ITEMWIDGET_H 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

ItemWidget.cpp

#include "itemwidget.h" #include "global.h" #include "ctooltip.h" #include <QEvent> #include <QCursor> ItemWidget::ItemWidget(QWidget *parent) : QWidget(parent) { labelIcon = new QLabel(this); labelName = new QLabel(this); labelName->setStyleSheet("QLabel{color: green; font: 13pt bold;}"); labelInfo = new QLabel(this); labelInfo->setStyleSheet("QLabel{color: gray;}"); verlayout = new QVBoxLayout(); verlayout->setContentsMargins(0, 0, 0, 0); verlayout->addWidget(labelName); verlayout->addWidget(labelInfo); horLayout = new QHBoxLayout(this); horLayout->setContentsMargins(2, 2, 2, 2); horLayout->addWidget(labelIcon, 1, Qt::AlignTop); horLayout->addLayout(verlayout, 4); } void ItemWidget::setText(QPixmap pixmap, QString name, QString info) { labelIcon->setPixmap(pixmap); labelName->setText(name); labelInfo->setText(info); } // 測試用的 void ItemWidget::setText(QString info) { labelIcon->setText(info); } // 鼠標懸停的時候,顯示當前用戶簡要信息 bool ItemWidget::event(QEvent *e) { if (e->type() == QEvent::ToolTip) { qDebug() << "tool tip show"; g_toolTip->showMessage(labelIcon->pixmap(), labelName->text(), labelInfo->text(), QCursor::pos()); } else if (e->type() == QEvent::Leave) { qDebug() << "tool tip leave"; g_toolTip->hide(); } return QWidget::event(e); } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

然后是CToolTip自定義樣式部分: 
CToolTip.h

#ifndef CTOOLTIP_H #define CTOOLTIP_H #include <QWidget> #include <QLabel> #include <QPushButton> #include <QGroupBox> #include <QVBoxLayout> #include <QHBoxLayout> class CToolTip : public QWidget { Q_OBJECT public: explicit CToolTip(QWidget *parent = 0); void showMessage(const QPixmap *pixmap, QString name, QString info, QPoint point); void showMessage(const QPixmap *pixmap, QPoint point); signals: public slots: private: QLabel *labelIcon; QLabel *labelName; QLabel *labelInfo; QHBoxLayout *horLayout; QVBoxLayout *verlayout; QGroupBox *groupBox; protected: void hoverEvent(QHoverEvent *); }; #endif // CTOOLTIP_H 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

CToolTip.cpp

#include "ctooltip.h" #include <QDebug> #include <QApplication> #include <QDesktopWidget> CToolTip::CToolTip(QWidget *parent) : QWidget(parent) { this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint); this->resize(200, 100); ; this->setObjectName("CToolTip"); this->setStyleSheet("QWidget#CToolTip {border: 2px solid green; }"); groupBox = new QGroupBox(this); groupBox->setGeometry(10, 10, 180, 80); groupBox->setTitle("用戶信息"); labelIcon = new QLabel(groupBox); labelName = new QLabel(groupBox); labelInfo = new QLabel(groupBox); verlayout = new QVBoxLayout(); verlayout->setContentsMargins(0, 0, 0, 0); verlayout->addWidget(labelName); verlayout->addWidget(labelInfo); horLayout = new QHBoxLayout(groupBox); horLayout->setContentsMargins(10, 10, 10, 10); horLayout->addWidget(labelIcon, 1, Qt::AlignTop); horLayout->addLayout(verlayout, 4); } // 顯示ToolTip消息 void CToolTip::showMessage(const QPixmap *pixmap, QString name, QString info, QPoint point) { labelIcon->setPixmap(*pixmap); labelName->setText(name); labelInfo->setText(info); // 重新定義CToolTip的坐標 int rectX; int rectY; if (point.rx() < 200) { rectX = point.rx() + 10; } else { rectX = point.rx() - 240; } rectY = point.ry(); move(QPoint(rectX, rectY)); QWidget::show(); } // 顯示ToolTip消息 void CToolTip::showMessage(const QPixmap *pixmap, QPoint point) { labelIcon->setPixmap(*pixmap); labelName->setText("自己想辦法獲取"); labelInfo->setText("自己動手,豐衣足食"); // 此處可以作為QToolTip樣式進行顯示 move(point); QWidget::show(); } // 當鼠標進入事件時,讓界面隱藏掉 void CToolTip::hoverEvent(QHoverEvent *) { hide(); } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

當然當然,在大多數的組件上面都有一個成員函數setToolTip(QSTring& ..) 
這個就可以實現簡單的友好信息提示功能了。

http://blog.csdn.net/u013007900/article/details/50224873


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM