试验方法:

先随便创几个带Widget的UI.

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

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

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

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

 

结果是:如描述那样:

Returns the visible child widget at the position (x, y) 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.  
  3. #include "childatfunction.h"  
  4. #include <QApplication>  
  5. #include <QTextCodec>  
  6.  
  7. int main(int argc, char *argv[])  
  8. {  
  9.  
  10.     QApplication a(argc, argv);  
  11.     QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));  
  12.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));  
  13.  
  14.     ChildAtFunction * childAtFunction = new ChildAtFunction();  
  15.     childAtFunction -> show();  
  16.  
  17.     return a.exec();  
  18. }  
  19.  
  20. /**  
  21. 运行结果:  
  22.  
  23. Address =  QLabel(0x50e420, name = "label")  
  24. Address =  QWidget(0x50e3c0, name = "widget_1")  
  25. Address =  QWidget(0x50e3e0, name = "widget_2")  
  26. Address =  QWidget(0x50e400, name = "widget_3")  
  27.  
  28. 按下鼠标后:  
  29. Catch Mouse Press Event:  QLabel(0x50e420, name = "label")  
  30. Catch Mouse Press Event:  QWidget(0x50e3e0, name = "widget_2")  
  31. Catch Mouse Press Event:  QWidget(0x50e400, name = "widget_3")  
  32. Catch Mouse Press Event:  QWidget(0x50e3c0, name = "widget_1")  
  33. Catch Mouse Press Event:  QObject(0x0)  
  34.  
  35. **/ 

 

  1. // childatfunction.h  
  2.  
  3. #ifndef CHILDATFUNCTION_H  
  4. #define CHILDATFUNCTION_H  
  5.  
  6. #include <QWidget>  
  7. #include <QEvent>  
  8. #include <QMouseEvent>  
  9. #include <QDebug>  
  10. #include "ui_Form.h"  
  11.  
  12. class ChildAtFunction : public QWidget, public Ui::Form  
  13. {  
  14.     Q_OBJECT  
  15.  
  16. public:  
  17.     explicit ChildAtFunction(QWidget *parent = 0);  
  18.  
  19. public slots:  
  20.     void    mousePressEvent ( QMouseEvent * event );  
  21. };  
  22.  
  23. #endif // CHILDATFUNCTION_H  

 

  1. // childatfunction.cpp  
  2.  
  3. #include "childatfunction.h"  
  4.  
  5. ChildAtFunction::ChildAtFunction(QWidget *parent) : QWidget(parent)  
  6. {  
  7.     setupUi(this);  
  8.     qDebug() << "Address = " << label;  
  9.     qDebug() << "Address = " << widget_1;  
  10.     qDebug() << "Address = " << widget_2;  
  11.     qDebug() << "Address = " << widget_3;  
  12. }  
  13.  
  14.  
  15. void ChildAtFunction::mousePressEvent ( QMouseEvent * event)  
  16. {  
  17.     qDebug() << "Catch Mouse Press Event: "<< childAt(event->pos());  
  18. }  

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>