免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3413 | 回复: 0
打印 上一主题 下一主题

简化实现qt中的信号阻塞blockSignals [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-12 11:58 |只看该作者 |倒序浏览
QT中经常会用到blockSignals, 且是成对出现,实现时常在函数的开始阻塞信号,在函数的结尾释放信号。
若里面有大量需要阻塞的变量对象,这样写是件很麻烦的事,代码也比较多。
如果能实现象下面的方式实现,这样能简化很多,且不关心释放问题.
下面的例子是关于slider与spinbox联动问题,若改slider影响spinbox,反之如此。

[代码] cpp代码
  1. void ImEffectWidget::slotSlider(int pos)
  2. {
  3. static int oldPos = 0;
  4. if (oldPos != pos)
  5. {
  6.   QRect r;
  7.   QObject *o = sender();
  8. //
  9.   ImSignalBlock block;
  10.   block << m_ui.spinBoxTransparent << m_ui.spinBoxVerticalPos
  11.      << m_ui.spinBoxHerizontalPos << m_ui.spinBoxSTTransparent
  12.      << m_ui.spinBoxSTVPos;
  13.   int tabType = m_treeItem->mdata("effect_catalog").toInt();
  14.   if (tabType == TAB_CATALOG_WATERMARK ){
  15.    r = m_treeItem->getEffectItem("effect_item_rect").toRect();
  16.   }

  17.   /* 3. for watermark  ***************************/
  18.   else if (o->objectName() == "sliderTransparent"){
  19.    m_ui.spinBoxTransparent->setValue(pos);  //与spinbox关联
  20.    m_treeItem->setEffectItem("effect_item_transparent", pos);
  21.   }else if (o->objectName() == "sliderVerticalPos"){
  22.    r.moveLeft(pos);
  23.    m_ui.spinBoxVerticalPos->setValue(pos);
  24.   }else if (o->objectName() == "sliderHorizontalPos"){
  25.    r.moveTop(pos);
  26.    m_ui.spinBoxHerizontalPos->setValue(pos);
  27.   }
  28.   /* 5. for subtitle  ***************************/
  29.   else if (o->objectName() == "sliderSTTransparent"){
  30.    m_ui.spinBoxSTTransparent->setValue(pos);
  31.    m_treeItem->setMediaData("effect_subtitle_transparent", pos);
  32.   }else if (o->objectName() == "sliderSTVPos"){
  33.    m_ui.spinBoxSTVPos->setValue(pos);
  34.    m_treeItem->setMediaData("effect_subtitle_pos", pos);
  35.   }

  36.   if (tabType == TAB_CATALOG_WATERMARK ){
  37.    m_treeItem->setEffectItem("effect_item_rect", r);
  38.   }
  39.   sendEffectEvent();   //向sdl发信号
  40.   oldPos = pos;
  41. }
  42. }
复制代码
[代码] ImSignalBlock.h
  1. #ifndef IMSIGNALBLOCK_H_
  2. #define IMSIGNALBLOCK_H_
  3. #include <QObject>
  4. class ImSignalBlock
  5. {
  6. public:
  7. ImSignalBlock();
  8. ~ImSignalBlock();
  9.   ImSignalBlock &operator<< (QObject *o);
  10. private:
  11. QList<QObject*> m_objs;
  12. };
  13. #endif
  14. ImSignalBlock.cpp:
  15. #include <QDebug>
  16. #include "ImSignalBlock.h"
  17. ImSignalBlock::ImSignalBlock()
  18. {
  19. }
  20. ImSignalBlock::~ImSignalBlock()
  21. {
  22. foreach (QObject* o, m_objs)
  23. {
  24.   if (o) o->blockSignals(false);
  25. }
  26. }
  27. ImSignalBlock &ImSignalBlock::operator<< (QObject *o)
  28. {
  29. if (o)
  30. {
  31.   m_objs.append(o);
  32.   qDebug() << o->objectName();
  33.   o->blockSignals(true);
  34. }
  35. return *this;
  36. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP