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 ...
随机推荐
- 在Xcode6中找回失去的模板
[Add]2014.07.27 添加OC category.protocol模板 Xcode 6从beta 3开始在创建新项目窗口中移除了“Empty Application”,如下: 其他选项很不幸 ...
- mac配置svn服务器
1创建svn库:mudydeMacBook-Air:~ mudy$ svnadmin create /Users/mudy/svn 2进行配置,配置我们的svn a.将svnsever.con ...
- Javascript版选择下拉菜单互移且排序
效果图如下: 代码如下: <html> <head> <title>Javascript版选择下拉菜单互移且排序</title> <meta ht ...
- MongoDB使用记录
安装服务 使用以下命令将MongoDB安装成为Windows服务.笔者的MongoDB目录为D:\Program Files\mongodb mongod --logpath "D:\Pro ...
- JSP九大内置对象(转载)
JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page.exception 1. ...
- JAVA多线程实现简单的点名系统
效果图如下: CMain函数: package com.shubing.main; public class CMain { public static void main(String[] args ...
- HDU 4768 Flyer(二分法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目大意:每组数据有n行输入,每行有三个数A.B.C,A<=B且小于2^32,从A到B每隔 ...
- Menu bar missing from ClearCase Explorer
See following links: Menu bar missing from ClearCase Explorer Understanding the Rational ClearCase E ...
- OpenJudge / Poj 1003 Hangover
链接地址: Poj:http://poj.org/problem?id=1003 OpenJudge:http://bailian.openjudge.cn/practice/1003 题目: Han ...
- Android AndroidManifest学习笔记
<application>标签 : android:allowBackup="true" 数据可以备份 <activity>标签:configChanges ...