• 11509阅读
  • 8回复

[讨论]Qt实现提示效果 [复制链接]

上一主题 下一主题
离线wangjieest
 

只看楼主 倒序阅读 楼主  发表于: 2012-10-11

请问如何在QTreeWidget里面实现Windows的这个效果呢?
使用Tooltip能很好的判断位置,但是有延迟,而且位置也对不上.
使用QMouseEvent  itemAt得到子项,又不知道如何判断文本宽度,还有不知道这个是如何显示出来的...raise?
离线jdwx

只看该作者 1楼 发表于: 2012-10-11
回 楼主(wangjieest) 的帖子
就是QToolTip
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线wangjieest

只看该作者 2楼 发表于: 2012-10-11
Re:回 楼主(wangjieest) 的帖子
引用第1楼jdwx于2012-10-11 22:00发表的 回 楼主(wangjieest) 的帖子 :
就是QToolTip


在MFC下,是有一个这样的bool选项的 ToolTips...
但是在Qt里,
我用QToolTip实现过,显示有延迟,位置也对不上号,隐藏也有延迟...
应该不是的吧

离线xlttap

只看该作者 3楼 发表于: 2012-10-12
回 2楼(wangjieest) 的帖子
明显是你自己用得不对。
我简单我快乐
离线wangjieest

只看该作者 4楼 发表于: 2012-10-12
回 3楼(xlttap) 的帖子
大侠有何高见,愿闻其详...

不过,别有事没事就来个“明显你的不对“,先那拿出个对的出来瞧瞧!
离线XChinux

只看该作者 5楼 发表于: 2012-10-12
自己写delegate实现helpEvent()函数自己响应QEvent::ToolTip来画。画成什么样的,自己处理。不过这里toolTip的响应时间依然没有解决,默认的是比较长的
用QEvent::HoverEnter    和QEvent::HoverLeave来自己发送QEvent::ToolTip信号,也可行否?然后回到第一句交由delegate的helpEvent()来处理。
自己画,也可以用QToolTip::showText()来吧,计算好位置就行。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线XChinux

只看该作者 6楼 发表于: 2012-10-12
下面是一段示例代码,是给一个QTreeView定义的delegate,根据鼠标在当前项中的详细位置不同显示不同的tooltip
  1. // 自定义ToolTip,请适时修改
  2. bool ContactStyledItemDelegate::helpEvent(QHelpEvent *event,
  3.         QAbstractItemView *view, const QStyleOptionViewItem &option,
  4.         const QModelIndex &index)
  5. {
  6.     if (event->type() == QEvent::ToolTip && index.isValid())
  7.     {
  8.         if (index.parent().isValid())
  9.         {
  10.             // 普通项,如果位置在工具按钮上,则显示工具按钮的WhatsThis
  11.             // 在初始化工具按钮时,要将其toolTip属性设置为空,这样它就不会显示
  12.             // 自己原来的工具提示了
  13.             QString strToolTip;
  14.              // PASS 初始化变量
  15.             QStyleOptionViewItemV4 opt = option;
  16.             initStyleOption(&opt, index);
  17.             {
  18.                 QStringList slIdList = index.data(ContactShowButtonIdList)
  19.                     .toStringList();
  20.                 if (!slIdList.isEmpty())
  21.                 {
  22.                     QStringList slTipList = index.data(
  23.                             ContactShowButtonTipList).toStringList();
  24.                     QRect rect(0, opt.rect.top() + 4,
  25.                             _toolIconSize.width(), _toolIconSize.height());
  26.                     int w = _toolIconSize.width() + 4;
  27.                     int x = opt.rect.right() - w * slIdList.size() + w - 4;
  28.                     QPoint curpos = event->pos();
  29.                     QRect xxx(x, rect.y(),
  30.                            opt.rect.right() - x, rect.height() + 4 * 2);
  31.                     for (int i = 0; i < slIdList.size(); i++)
  32.                     {
  33.                         rect.moveRight(x + w * i);
  34.                         if (rect.contains(curpos))
  35.                         {
  36.                             strToolTip = slTipList[i];
  37.                             break;
  38.                         }
  39.                     }
  40.                     if (strToolTip.isEmpty() && xxx.contains(curpos))
  41.                     {
  42.                         // 如果当前鼠标在按钮整体区域中间,但没有在任何按钮上
  43.                         // 则不显示任何提示
  44.                         return false;
  45.                     }
  46.                 }
  47.                 slIdList = index.data(ContactShowButton2IdList)
  48.                     .toStringList();
  49.                 if (!slIdList.isEmpty())
  50.                 {
  51.                     QStringList slTipList = index.data(
  52.                             ContactShowButton2TipList).toStringList();
  53.                     QRect rect(0,
  54.                             opt.rect.bottom() - _toolIconSize.height() - 4,
  55.                             _toolIconSize.width(), _toolIconSize.height());
  56.                     int w = _toolIconSize.width() + 4;
  57.                     int x = opt.rect.right() - w * slIdList.size() + w - 4;
  58.                     QPoint curpos = event->pos();
  59.                     QRect xxx(x, rect.y(),
  60.                            opt.rect.right() - x, rect.height() + 4 * 2);
  61.                     for (int i = 0; i < slIdList.size(); i++)
  62.                     {
  63.                         rect.moveRight(x + w * i);
  64.                         if (rect.contains(curpos))
  65.                         {
  66.                             strToolTip = slTipList[i];
  67.                             break;
  68.                         }
  69.                     }
  70.                     if (strToolTip.isEmpty() && xxx.contains(curpos))
  71.                     {
  72.                         // 如果当前鼠标在按钮整体区域中间,但没有在任何按钮上
  73.                         // 则不显示任何提示
  74.                         return false;
  75.                     }
  76.                 }
  77.             }
  78.             if (strToolTip.isEmpty())
  79.             {
  80.                 const QWidget *widget = opt.widget;
  81.                 QStyle *style = widget ? widget->style() : QApplication::style();
  82.                 QRect iconRect = style->subElementRect(
  83.                         QStyle::SE_ItemViewItemDecoration, &opt, widget);
  84.                 int x2 = iconRect.right() + iconRect.x();
  85.                 if (event->pos().x() < x2)
  86.                 {
  87.                     emit iconTipRequested(index);
  88.                     return true;
  89.                 }
  90.             }
  91.             if (strToolTip.isEmpty())
  92.             {
  93.                 strToolTip = QString("Name: %1\n"
  94.                         "Description: %2")
  95.                     .arg(index.data(ContactName).toString())
  96.                     .arg(index.data(ContactDesp).toString());
  97.             }
  98.             QToolTip::showText(event->globalPos(), strToolTip);
  99.         }
  100.         else
  101.         {
  102.             // 分组项
  103.             QToolTip::showText(event->globalPos(),
  104.                     QString("Group Name: %1\n"
  105.                         "All Contacts: %2\n"
  106.                         "Online Contacts: %3")
  107.                     .arg(index.data(GroupName).toString())
  108.                     .arg(index.data(GroupAllNum).toInt())
  109.                     .arg(index.data(GroupOnlineNum).toInt()));
  110.         }
  111.         return true;
  112.     }
  113.     else
  114.     {
  115.         return false;
  116.     }
  117. }

二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线wangjieest

只看该作者 7楼 发表于: 2012-10-12
引用第6楼XChinux于2012-10-12 17:12发表的  :
下面是一段示例代码,是给一个QTreeView定义的delegate,根据鼠标在当前项中的详细位置不同显示不同的tooltip
[code]// 自定义ToolTip,请适时修改
bool ContactStyledItemDelegate::helpEvent(QHelpEvent *event,
        QAbstractItemView *view, const QStyleOptionViewItem &option,
        const QModelIndex &index)
.......


总版吉祥
还刚接触 Model/View ,先试着自己理解一遍,再来请教总版..
离线wangjieest

只看该作者 8楼 发表于: 2012-10-15
其实现在的关键是,
1. 是否存在 对应 Item 的 enterEvent 和 leaveEvent?
2. 如何获取相应的rect.这个是委托来的,rect貌似不在委托里面..
3. 如何得到显示出来的rect和实际文字长度的比较...


我觉得还是在view里面控制比较好,Model是数据,Delegate是编辑控件,而我要的是enter和leave时显示...


现在的难题是,
view如何得知item的rect...
view如何得知item文字区域的rect,或者文字区域前面的rect...因为这个需要判断文字是否被遮盖了....
view上对于item的enter和leave事件怎么样添加比较好呢?
快速回复
限100 字节
 
上一个 下一个