试验方法:

先随便创几个带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.