试验方法:

先随便创几个带Widget的UI.

然后,创建使用这个UI的类。

之后,构造时显示这些UI里面Widget的内存地址信息。

创建事件截取的函数,试试ChildAt()函数的运行效果。

最后,点击UI中的Widget观察输出,得出结果。

结果是:如描述那样:

Returns the visible child widget at the position (xy) in the widget's coordinate system. If there is no visible child widget at the specified position, the function returns 0.

源代码:

  1. // main.cpp
  2. #include "childatfunction.h"
  3. #include <QApplication>
  4. #include <QTextCodec>
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
  9. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
  10. ChildAtFunction * childAtFunction = new ChildAtFunction();
  11. childAtFunction -> show();
  12. return a.exec();
  13. }
  14. /**
  15. 运行结果:
  16. Address =  QLabel(0x50e420, name = "label")
  17. Address =  QWidget(0x50e3c0, name = "widget_1")
  18. Address =  QWidget(0x50e3e0, name = "widget_2")
  19. Address =  QWidget(0x50e400, name = "widget_3")
  20. 按下鼠标后:
  21. Catch Mouse Press Event:  QLabel(0x50e420, name = "label")
  22. Catch Mouse Press Event:  QWidget(0x50e3e0, name = "widget_2")
  23. Catch Mouse Press Event:  QWidget(0x50e400, name = "widget_3")
  24. Catch Mouse Press Event:  QWidget(0x50e3c0, name = "widget_1")
  25. Catch Mouse Press Event:  QObject(0x0)
  26. **/
  1. // childatfunction.h
  2. #ifndef CHILDATFUNCTION_H
  3. #define CHILDATFUNCTION_H
  4. #include <QWidget>
  5. #include <QEvent>
  6. #include <QMouseEvent>
  7. #include <QDebug>
  8. #include "ui_Form.h"
  9. class ChildAtFunction : public QWidget, public Ui::Form
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit ChildAtFunction(QWidget *parent = 0);
  14. public slots:
  15. void    mousePressEvent ( QMouseEvent * event );
  16. };
  17. #endif // CHILDATFUNCTION_H
  1. // childatfunction.cpp
  2. #include "childatfunction.h"
  3. ChildAtFunction::ChildAtFunction(QWidget *parent) : QWidget(parent)
  4. {
  5. setupUi(this);
  6. qDebug() << "Address = " << label;
  7. qDebug() << "Address = " << widget_1;
  8. qDebug() << "Address = " << widget_2;
  9. qDebug() << "Address = " << widget_3;
  10. }
  11. void ChildAtFunction::mousePressEvent ( QMouseEvent * event)
  12. {
  13. qDebug() << "Catch Mouse Press Event: "<< childAt(event->pos());
  14. }

UI:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Form</class>
  4. <widget class="QWidget" name="Form">
  5. <property name="geometry">
  6. <rect>
  7. <x>0</x>
  8. <y>0</y>
  9. <width>462</width>
  10. <height>308</height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>Form</string>
  15. </property>
  16. <widget class="QWidget" name="widget_1" native="true">
  17. <property name="geometry">
  18. <rect>
  19. <x>160</x>
  20. <y>10</y>
  21. <width>120</width>
  22. <height>80</height>
  23. </rect>
  24. </property>
  25. <property name="palette">
  26. <palette>
  27. <active>
  28. <colorrole role="Base">
  29. <brush brushstyle="SolidPattern">
  30. <color alpha="255">
  31. <red>255</red>
  32. <green>255</green>
  33. <blue>255</blue>
  34. </color>
  35. </brush>
  36. </colorrole>
  37. <colorrole role="Window">
  38. <brush brushstyle="SolidPattern">
  39. <color alpha="255">
  40. <red>99</red>
  41. <green>240</green>
  42. <blue>186</blue>
  43. </color>
  44. </brush>
  45. </colorrole>
  46. </active>
  47. <inactive>
  48. <colorrole role="Base">
  49. <brush brushstyle="SolidPattern">
  50. <color alpha="255">
  51. <red>255</red>
  52. <green>255</green>
  53. <blue>255</blue>
  54. </color>
  55. </brush>
  56. </colorrole>
  57. <colorrole role="Window">
  58. <brush brushstyle="SolidPattern">
  59. <color alpha="255">
  60. <red>99</red>
  61. <green>240</green>
  62. <blue>186</blue>
  63. </color>
  64. </brush>
  65. </colorrole>
  66. </inactive>
  67. <disabled>
  68. <colorrole role="Base">
  69. <brush brushstyle="SolidPattern">
  70. <color alpha="255">
  71. <red>99</red>
  72. <green>240</green>
  73. <blue>186</blue>
  74. </color>
  75. </brush>
  76. </colorrole>
  77. <colorrole role="Window">
  78. <brush brushstyle="SolidPattern">
  79. <color alpha="255">
  80. <red>99</red>
  81. <green>240</green>
  82. <blue>186</blue>
  83. </color>
  84. </brush>
  85. </colorrole>
  86. </disabled>
  87. </palette>
  88. </property>
  89. <property name="autoFillBackground">
  90. <bool>true</bool>
  91. </property>
  92. </widget>
  93. <widget class="QWidget" name="widget_2" native="true">
  94. <property name="geometry">
  95. <rect>
  96. <x>150</x>
  97. <y>130</y>
  98. <width>120</width>
  99. <height>80</height>
  100. </rect>
  101. </property>
  102. <property name="palette">
  103. <palette>
  104. <active>
  105. <colorrole role="Base">
  106. <brush brushstyle="SolidPattern">
  107. <color alpha="255">
  108. <red>255</red>
  109. <green>255</green>
  110. <blue>255</blue>
  111. </color>
  112. </brush>
  113. </colorrole>
  114. <colorrole role="Window">
  115. <brush brushstyle="SolidPattern">
  116. <color alpha="255">
  117. <red>99</red>
  118. <green>240</green>
  119. <blue>186</blue>
  120. </color>
  121. </brush>
  122. </colorrole>
  123. </active>
  124. <inactive>
  125. <colorrole role="Base">
  126. <brush brushstyle="SolidPattern">
  127. <color alpha="255">
  128. <red>255</red>
  129. <green>255</green>
  130. <blue>255</blue>
  131. </color>
  132. </brush>
  133. </colorrole>
  134. <colorrole role="Window">
  135. <brush brushstyle="SolidPattern">
  136. <color alpha="255">
  137. <red>99</red>
  138. <green>240</green>
  139. <blue>186</blue>
  140. </color>
  141. </brush>
  142. </colorrole>
  143. </inactive>
  144. <disabled>
  145. <colorrole role="Base">
  146. <brush brushstyle="SolidPattern">
  147. <color alpha="255">
  148. <red>99</red>
  149. <green>240</green>
  150. <blue>186</blue>
  151. </color>
  152. </brush>
  153. </colorrole>
  154. <colorrole role="Window">
  155. <brush brushstyle="SolidPattern">
  156. <color alpha="255">
  157. <red>99</red>
  158. <green>240</green>
  159. <blue>186</blue>
  160. </color>
  161. </brush>
  162. </colorrole>
  163. </disabled>
  164. </palette>
  165. </property>
  166. <property name="autoFillBackground">
  167. <bool>true</bool>
  168. </property>
  169. </widget>
  170. <widget class="QWidget" name="widget_3" native="true">
  171. <property name="geometry">
  172. <rect>
  173. <x>300</x>
  174. <y>80</y>
  175. <width>120</width>
  176. <height>80</height>
  177. </rect>
  178. </property>
  179. <property name="palette">
  180. <palette>
  181. <active>
  182. <colorrole role="Base">
  183. <brush brushstyle="SolidPattern">
  184. <color alpha="255">
  185. <red>255</red>
  186. <green>255</green>
  187. <blue>255</blue>
  188. </color>
  189. </brush>
  190. </colorrole>
  191. <colorrole role="Window">
  192. <brush brushstyle="SolidPattern">
  193. <color alpha="255">
  194. <red>99</red>
  195. <green>240</green>
  196. <blue>186</blue>
  197. </color>
  198. </brush>
  199. </colorrole>
  200. </active>
  201. <inactive>
  202. <colorrole role="Base">
  203. <brush brushstyle="SolidPattern">
  204. <color alpha="255">
  205. <red>255</red>
  206. <green>255</green>
  207. <blue>255</blue>
  208. </color>
  209. </brush>
  210. </colorrole>
  211. <colorrole role="Window">
  212. <brush brushstyle="SolidPattern">
  213. <color alpha="255">
  214. <red>99</red>
  215. <green>240</green>
  216. <blue>186</blue>
  217. </color>
  218. </brush>
  219. </colorrole>
  220. </inactive>
  221. <disabled>
  222. <colorrole role="Base">
  223. <brush brushstyle="SolidPattern">
  224. <color alpha="255">
  225. <red>99</red>
  226. <green>240</green>
  227. <blue>186</blue>
  228. </color>
  229. </brush>
  230. </colorrole>
  231. <colorrole role="Window">
  232. <brush brushstyle="SolidPattern">
  233. <color alpha="255">
  234. <red>99</red>
  235. <green>240</green>
  236. <blue>186</blue>
  237. </color>
  238. </brush>
  239. </colorrole>
  240. </disabled>
  241. </palette>
  242. </property>
  243. <property name="autoFillBackground">
  244. <bool>true</bool>
  245. </property>
  246. </widget>
  247. <widget class="QLabel" name="label">
  248. <property name="geometry">
  249. <rect>
  250. <x>30</x>
  251. <y>90</y>
  252. <width>71</width>
  253. <height>71</height>
  254. </rect>
  255. </property>
  256. <property name="text">
  257. <string>TextLabel</string>
  258. </property>
  259. </widget>
  260. </widget>
  261. <resources/>
  262. <connections/>
  263. </ui>
 

转载于:https://blog.51cto.com/neicole/970107

QWidget中的ChildAt()函数使用试验相关推荐

  1. 【Qt】QWidget类详解(函数篇)

    00. 目录 文章目录 00. 目录 01. 概述 02. Reimplemented Public Functions 03. Public Slots 04. Signals 05. Static ...

  2. QT学习-界面中实时绘制函数图像

    通过重写QT中QWidget类中的paintEvent函数,我们就可以做到在widget中进行函数图像的绘制.(我使用的是QCreator的UI设计器) 首先我们需要从QWidget继承一个子类,并重 ...

  3. OpenCV 相机校正过程中,calibrateCamera函数projectPoints函数的重投影误差的分析

    OpenCV 校正过程中,calibrateCamera函数的ret和重投影误差的分析 OpenCV对相机进行校正的过程中,校正返回值retval和重投影误差的计算公式表示和分析. OpenCV 校正 ...

  4. Iar环境c语言调用汇编函数,如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数?...

    我在硬故障处理程序中有一些程序集.程序集基本上是为了传递当前堆栈指针作为参数(在R0中).它看起来像这样...如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数? __asm(&quo ...

  5. C++11 :STL中的 iota ()函数

    该函数是C++11 才引入,之前版本没有此函数. iota 函数是一个计算机语言中的函数,用于产生连续的值.该函数得名自 APL 语言,其中用来产生从 1 开始的连续数值. 该函数位于头文件#incl ...

  6. Matlab中的lsqcurvefit函数的使用

    Matlab中的lsqcurvefit函数的使用 lsqcurvefit函数 调用示例 lsqcurvefit函数 非线性曲线拟合是已知输入向量xdata和输出向量ydata,并且知道输入与输出的函数 ...

  7. 在Vue的webpack中结合runder函数

    在Vue的webpack中结合runder函数 1.引入: <h1>下面是vue的内容:</h1><div id="app"><login ...

  8. 【OpenCV】OpenCV中积分图函数与应用

    OpenCV中积分图函数与应用 参考资料 opencv 查找integral,目前网上大部分的资料来自于opencv https://docs.opencv.org/master/d7/d1b/gro ...

  9. Oralce中的to_date()函数

    Oralce中的to_date()函数   to_date( '".$params['ORDER_TIME']."','YYYY-MM-DD') TO_DATE(:BEGIN_DA ...

最新文章

  1. PKI与证书服务应用以及相关安全协议
  2. Java程序员春招三面蚂蚁金服,1200页文档笔记
  3. 虚拟机linux中怎样打开qt,虚拟机中在Centos 4.7中安装qt-x11-opensource-4.4.3
  4. 烟台大学计算机专业调剂贴吧,烟台大学计算机与控制工程学院2021年考研复试与调剂的说明...
  5. 怎么才能升级成鸿蒙系统,怎么能升级成鸿蒙系统
  6. python 回溯法 01背包问题_回溯法解决01背包问题
  7. 对Spring的IoC和DI最生动的解释
  8. 深入理解redis复制原理
  9. 机器学习基础算法11-Logistic回归-ROC和AUC分类模型评估-实例
  10. 【推荐】HTML5 Word Cloud——中文词云
  11. Nature呼吁:应对21世纪的大规模流行病,各个学科必须联合起来
  12. wps——ppt中的视频提取
  13. html点击按钮弹出悬浮窗_html弹窗,html网页弹窗代码
  14. 旧约圣经对基督(弥赛亚)的预言
  15. markdown的各种操作
  16. 在服务器创建并进入虚拟环境
  17. android 旋转屏幕 不重走生命周期,屏幕旋转后Activity生命周期
  18. C++ 判断路径是否存在,不存在则创建(包含多级创建)
  19. 什么叫显示动力学,什么叫隐式动力学分析!
  20. 微信小程序js把数字转化成字母

热门文章

  1. asa802.k8-telnet for lan-base
  2. eclipse新建Android项目时提示AndroidManifest.xml file missing!
  3. struts2.xml中使用chain和redirectAction这两个注意事项
  4. 如何计算java对象占用的内存
  5. 批评一下 dearbook
  6. POJ 3471 Integral Roots(素数、因数)
  7. java 开发银行支付、对账时证书相关的操作总结
  8. JAXB:Java对象序和XML互相转化的利器
  9. GIF 太大?用 GIFSicle
  10. html 5 新增标签及简介