QAbstractItemView为截断的项显示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(使用事件过滤)的更多相关文章
- QAbstractItemView为截断的项显示ToolTip(在eventFilter函数里覆盖QEvent::ToolTip事件)
在Qt中想要为QAbstractItemView中长度不够而使得内容被截断的项显示ToolTip,Qt官网有一篇文章介绍使用事件过滤器来显示太长的项,但是没有涵盖图标的情况.显示列头项太长的情况等等, ...
- css截断长文本显示
实现 截断长文本显示处理,以前是通过后台的截取,但这种方法容易丢失数据,不利于SEO. 而通过前端css的截断,则灵活多变,可统一运用与整个网站. 这项技术主要运用了text-overflow属性,这 ...
- 在Winfrom 中,如何实现combox 的列表自动显示ToolTip提示 ?
//带ToolTip的combox类文件 public class ComboBoxWithTooltip : ComboBox { //tipProperty为显示ToolTip文本的数据源的属性 ...
- C++ CEF 浏览器中显示 Tooltip(标签中的 title 属性)
在 Windows 中将 CEF 集成到 C++ 客户端以后,默认是无法显示 tooltip 的,比如图片标签中的 title 属性. 实现的方式其实很简单,按下面的步骤操作就可以: 创建一个文本文件 ...
- 【UWP】仅在TextBlock文本溢出时显示Tooltip
前言 这是我今天在回答SO问题时偶然遇到的,觉得可能还比较通用,就记录下来以供参考. 通常,我们使用ToolTip最简单的方式是这样: <TextBlock Text="Test&qu ...
- dotnet ef执行报错, VS 2019发布时配置项中的Entity Framework迁移项显示不出来
VS 2019发布时配置项中的Entity Framework迁移项显示不出来 dotnet ef dbcontext list --json “无法执行,因为找不到指定的命令或文件.可能的原因包括: ...
- WPF drag过程中显示ToolTip.
原文:WPF drag过程中显示ToolTip. 在drag/drop过程中,我们在判断出over的元素上是否可以接受drag的东西之后,通常是通过鼠标的样式简单告诉用户这个元素不接受现在drag的内 ...
- ListView控件的列表项的文字不满一行的时候,如何实现点击该列表项的空白区域仍可触发列表项的点击事件
今天在做Demo的过程中,使用到了ListView.然而在实现过程中,发现一个出现了一个问题:只能点击列表项的文字区域可以触发点击事件,而点击列表项的空白区域无法触发点击事件. 如下图: listit ...
- NGUI---使用脚本控制聊天系统的内容显示,输入事件交互
在我的笔记Unity3D里面之 简单聊天系统一 里面已经介绍怎么创建聊天系统的背景.给聊天系统添加滚动条,设置Anchor锚点.以及设计聊天系统的输入框. 效果图如下所示: 现在我们要做的就是使用脚本 ...
随机推荐
- hbase 2.0.2 分布式安装配置/jar包替换
环境 zk: 3.4.10 hadoop 2.7.7 jdk8 hbase 2.0.2 三台已安装配置好的hadoop002,hadoop003,hadoop004 1.上传并解压hbase-2.1. ...
- 【25.47%】【codeforces 733D】Kostya the Sculptor
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- React Native中的DeviceEventEmitter.addListener与DeviceEventEmitter.emit
官方文档没有对这两个方法做很好的解释,需要自己找资料研究.看了几篇文章,总结是和订阅发布模式差不多,用来事件监听发送的. React Native学习之DeviceEventEmitter传值 R ...
- Arcgis api for javascript学习笔记-控制地图缩放比例尺范围(3.2X版本与4.6版本)
Ⅰ. 在3.X版本中,设置Map对象的 "maxScale" 和 "minScale" 属性 <!DOCTYPE html> <html> ...
- Numpy Usage Introduction
Reference : http://my.oschina.net/u/175377/blog/74406 试验性的Numpy教程 原文来自Tentative NumPy Tutorial 目录 先决 ...
- node lesson6
https://nodejs.org/docs/latest/api/process.html#process_process_argv https://github.com/alsotang/nod ...
- Java中,对多线程访问同一变量(并发访问)的认识
在Java中,如果启动多个线程对同一个对象或者变量时候,在没有安全保护前提下有可能会抛出并异常 java.util.ConcurrentModificationException 当方法检测到对象的并 ...
- MyEclipse迅速
MyEclipse迅速 1.详细例如以下图 2.提示原因 3.解决方案 版权声明:本文博主原创文章.博客,未经同意不得转载.
- 关于java中继承抽象类和实现接口的区别
简单来说,继承就是“是不是”,实现就是“有没有”.(一个大神说的,我觉得很生动很形象 海子大神链接http://www.cnblogs.com/dolphin0520/p/3811437.html)
- Information Centric Networking Based Service Centric Networking
A method implemented by a network device residing in a service domain, wherein the network device co ...