在日常生活中的使用的软件中,我们经常会遇到这样的情况。 
我们在网页上,有些网页链接的文字(比如文章标题,知乎问题标题,百度的词条等)因为太长了,而显示不出来,但是鼠标悬停在上面的时候就可以显示出来。 
我们在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

Qt5制作鼠标悬停显示Hint的ToolTip的更多相关文章

  1. EasyUI Datagrid 鼠标悬停显示单元格内容 复制代码

    EasyUI Datagrid 鼠标悬停显示单元格内容 ,halign:, align: 0 « 上一篇:LINQ to Entities 中的查询» 下一篇:去掉字符串中的非数字字符 posted ...

  2. 鼠标悬停显示CSS3动画边框

    效果体验:http://keleyi.com/keleyi/phtml/css3/14.htm 以下是代码: <!DOCTYPE html> <html xmlns="ht ...

  3. Android Studio鼠标悬停显示注释

    Android Studio鼠标悬停显示注释 在AS中配置 如果你想从网上查看注释,到这一步就操作完成. 下面说明让软件使用本地注释: 使用本地注释 以Windows为例: 找到下面文件 C:\Use ...

  4. delphi treeview 鼠标移动显示hint信息

    procedure TForm1.TreeView1MouseMove(Sender: TObject; Shift: TShiftState;   X, Y: Integer); var   Nod ...

  5. EasyUI的Datagrid鼠标悬停显示单元格内容

    功能描述:table鼠标悬停显示单元格内容 1.js函数 function hoveringShow(value) { return "<span title='" + va ...

  6. Bootstrap学习笔记(5)--实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单

    实现Bootstrap导航条可点击和鼠标悬停显示下拉菜单 微笑的鱼 2014-01-03 Bootstrap 5,281 次围观 11条评论 使用Bootstrap导航条组件时,如果你的导航条带有下拉 ...

  7. jQuery实现鼠标悬停显示提示信息窗口的方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 可以兼容ie6的纯CSS三级鼠标悬停显示/隐藏菜单实现

    本来在chrome上用js写的好好的三级显隐菜单,放到ie6上一测试竟然奇葩般的会瞎闪.问题原因至今没参透,可能是我每次响应事件的处理代码过长??总之我是对ie6幻灭了,去网上搜一搜能支持ie6的下拉 ...

  9. jQuery鼠标悬停显示提示信息窗体

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. IE能够打开网页 可是chrome和火狐打不开网页解决的方法

    一次偶然.电脑的浏览器打不开经常使用的网页,奇怪的是IE能够打开 之外的其它浏览器都不能够,结果百度一下.找到一个帖子,亲自測试一下,果真能够解决.记录例如以下: (1)開始-执行-输入CMD-确定- ...

  2. 关于js封装框架类库之事件模块

    在触发DOM上的某个事件时,会产生一个事件对象event.这个对象中包含着所有与事件有关的信息.包括导致事件的元素,事件的类型以及其他与特定事件相关的信息. 例如: 鼠标操作点击事件时,事件对象中会获 ...

  3. hibernate -inverse

    one to many inverse=false只能设置维护关联关系的多的一方, inverse属性: 默认为false,表示本方维护关联关系. 如果为true,表示本方不维护关联关系(并不意味着对 ...

  4. BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )

    贪心...用最弱的赢最弱的,用最强的赢最强的,否则用最弱的和最强的比... (贴个官方题解:将双方的选手均按从强到弱排序,然后第一次扫描尽可能用当前剩下的选手中能赢对手当前最强选手中最弱的一个去赢得胜 ...

  5. JQuery中文本框获取焦点

    今天遇见这么一个小小的问题,就是文本框中需要输入内容才可以提交,如果没有输入就提示并使该文本框获得焦点! 这么一个简单的事情如果没有使用jQuery的话 是不是对象.focus()就可以了, 可是当我 ...

  6. Linux Mysql Client 查询中文乱码

    1.mysql client 端设置编码为utf8 set character_set_results=utf8; 2.连接linux的客户端的编码也要设置为utf8(比如xshell,putty等)

  7. 字符串匹配算法1-KMP

    前面介绍过,字符串搜索一般来说有三种方式,前缀搜索,后缀搜索,子串搜索.KMP使用的是前缀搜索. 假设p的偏移是i,也就是窗口的位置是i,匹配到位置j+1时发现了不匹配.现在的问题是向前移动窗口到什么 ...

  8. C和指针 读书笔记

    准备复习一下之前读过的<C和指针>,主要看之前标记过的地方. 感觉像第一次看的地方再记录一下-- 1.预处理器读入源代码,根据预处理指令对其进行修改,然后将修改后的源代码交给编译器. 2. ...

  9. npm常用命令详解

    ~~~https://www.npmjs.org/有比较全的api文档 NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的 ...

  10. FastJson中@JSONField注解使用

    最近做项目中,使用了json格式在服务器之间进行数据传输.但是发现json格式数据不符合JAVA中的变量定义规则,并且难以理解,因此需要在后台中做二次处理,将数据处理成我们系统中定义的格式. 思路: ...