QAbstractItemView为截断的项显示ToolTip(在eventFilter函数里覆盖QEvent::ToolTip事件)
在Qt中想要为QAbstractItemView中长度不够而使得内容被截断的项显示ToolTip,Qt官网有一篇文章介绍使用事件过滤器来显示太长的项,但是没有涵盖图标的情况、显示列头项太长的情况等等,这里做了下修改,以符合现在所需。
环境:Qt 5.1.0
atooltipper.h
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#ifndef ATOOLTIPPER_H
#define ATOOLTIPPER_H #include <QObject> class AToolTipper : public QObject protected: #endif // ATOOLTIPPER_H |
atooltipper.cpp
|
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
#include "atooltipper.h"
#include <QHelpEvent> #include <QAbstractItemView> #include <QHeaderView> #include <QTreeView> #include <QTableView> #include <QToolTip> AToolTipper::AToolTipper(QObject *parent) : bool AToolTipper::eventFilter(QObject *obj, QEvent *event) QHeaderView *headerView = qobject_cast<QHeaderView*>(view); QHelpEvent *helpEvent = static_cast<QHelpEvent*>(event); QString itemText = view->model()->data(index).toString(); // 图标处理 // 计算列头高度 // 文本边距 QFontMetrics fm(view->font()); if ((itemTextSize.width() > textRect.width() || itemTextSize.height() > textRect.height()) && !itemText.isEmpty()) return false; bool AToolTipper::headerViewEventFilter(QObject *obj, QEvent *event) QHelpEvent *helpEvent = static_cast<QHelpEvent*>(event); QString itemText = headerView->model()->headerData(index, headerView->orientation()).toString(); QFontMetrics fm(headerView->font()); return false; |
使用示例:
|
1
2 |
tableView->viewport()->installEventFilter(new AToolTipper(tableView));
tableView->horizontalHeader()->viewport()->installEventFilter(new AToolTipper(tableView->horizontalHeader())); |
效果图:
参考资料:
1.Show_tooltips_for_long_entries_of_your_custom_model http://qt-project.org/wiki/Show_tooltips_for_long_entries_of_your_custom_model
2.Tooltips for truncated items in a QTreeView http://www.mimec.org/node/337
http://blog.csdn.net/akof1314/article/details/14504747
QAbstractItemView为截断的项显示ToolTip(在eventFilter函数里覆盖QEvent::ToolTip事件)的更多相关文章
- QAbstractItemView为截断的项显示ToolTip(使用事件过滤)
在Qt中想要为QAbstractItemView中长度不够而使得内容被截断的项显示ToolTip,Qt官网有一篇文章介绍使用事件过滤器来显示太长的项,但是没有涵盖图标的情况.显示列头项太长的情况等等, ...
- css截断长文本显示
实现 截断长文本显示处理,以前是通过后台的截取,但这种方法容易丢失数据,不利于SEO. 而通过前端css的截断,则灵活多变,可统一运用与整个网站. 这项技术主要运用了text-overflow属性,这 ...
- dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来
VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...
- echarts 自定义配置带单位的 tooltip 提示框方法 和 圆环数据 tooltip 过长超出屏幕
-------tip1-------- 在 tooltip 里边配置:拼接字符串: tooltip : { trigger: 'axis', formatter:function(params) { ...
- 解决:npm中 下载速度慢 和(无法将“nrm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确, 然后再试一次)。
1.解决下载速度 因为我们npm下载默认是,连接国外的服务器,所以网速不是特别好的时候,可能下不了包 安装nrm 使用 npm i nrm -g 我们的一般工具包都是下载到全局 安装完毕之后,可以运行 ...
- VSCode下,项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
vscode下webpack错误:无法将“webpack”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. 解决方法: 1.因为 ...
- visual studio code运行时报错,无法将“cnpm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称,Cannot find module 'webpack'
前言 因公司技术需求,这段时间成功进入了Vue 2.0 的坑,刚用起Visual Studio Code,却发现问题很多,发现一个错误:cnpm : 无法将“cnpm”项识别为 cmdlet.函数.脚 ...
- '无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称' 或 'vue不是内部或外部命令' 的解决方法
如果在使用 vue 初始化项目的时候提示: vue : 无法将“vue”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. 或者: ...
- 无法将“Scaffold-DbContext”项识别为 cmdlet、函数、脚本文件或可运行程序的名称...
原文链接:https://my.oschina.net/taadis/blog/889560 为什么80%的码农都做不了架构师?>>> PM> Scaffold-DbC ...
随机推荐
- openlayers wfs获取要素
var wfsProtocol = new OpenLayers.Protocol.WFS.v1_1_0({ url: mapServerUrl + "/wfs", feature ...
- POJ 2777(线段树)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42507 Accepted: 12856 Des ...
- 基于libuv库的UDP收/发广播消息代码实现
uv_send(发送端): #include "uv.h" #include "task.h" #include <stdio.h> #includ ...
- centos 安装qrcode 二维码
先安装yum install mingw64-pkg-config.x86_64 yum install cairo-devel 然后报错,好像是gcc版本有点低,现在的版本是4.4.7 那么接下来 ...
- Oracle procedure存储过程/function函数
--函数的创建 create function func1(dno number) return NUMBER--必须带有返回值 is v_max number;--定义返回值 begin selec ...
- H5 APP开发必读,20个你不知道的Html5新特征和窍门
Jeffrey Way曾发表过一篇博文<28 HTML5 Features, Tips, and Techniques you Must Know >讲述了28个HTML5特征.窍门和技术 ...
- Pascal、VB、C#、Java四种语法对照表
因为工作原因,自学会了vb后陆续接触了其它语言,在工作中经常需要与各家使用不同语言公司的开发人员做程序对接,初期特别需要一个各种语法的对照比,翻看了网络上已有高人做了整理,自己在他基础上也整理了一下, ...
- configure: error: zlib library and headers are required
configure: error: zlib library and headers are required (1)直接看是zlib没安装导致的,yum list |grep zlib* 看到的是全 ...
- json在线校验
弄了一个在线校验,清爽无广告,欢迎大家收藏 http://www.zhhoney.com/
- C语言字符知识狭区
C语言字符在用户接口软件编程上经常用到,但是有一些狭区会让编程出现一些小BUG,现在总结与此. 1.'\\' 代表的是字符\,而'\'是不能代表字符\的.通常\后面都要跟上数字或者其他字母来表示一个特 ...