QWidget中的ChildAt()函数使用试验
试验方法:
先随便创几个带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.
源代码:
- // main.cpp
- #include "childatfunction.h"
- #include <QApplication>
- #include <QTextCodec>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
- QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
- ChildAtFunction * childAtFunction = new ChildAtFunction();
- childAtFunction -> show();
- return a.exec();
- }
- /**
- 运行结果:
- Address = QLabel(0x50e420, name = "label")
- Address = QWidget(0x50e3c0, name = "widget_1")
- Address = QWidget(0x50e3e0, name = "widget_2")
- Address = QWidget(0x50e400, name = "widget_3")
- 按下鼠标后:
- Catch Mouse Press Event: QLabel(0x50e420, name = "label")
- Catch Mouse Press Event: QWidget(0x50e3e0, name = "widget_2")
- Catch Mouse Press Event: QWidget(0x50e400, name = "widget_3")
- Catch Mouse Press Event: QWidget(0x50e3c0, name = "widget_1")
- Catch Mouse Press Event: QObject(0x0)
- **/
- // childatfunction.h
- #ifndef CHILDATFUNCTION_H
- #define CHILDATFUNCTION_H
- #include <QWidget>
- #include <QEvent>
- #include <QMouseEvent>
- #include <QDebug>
- #include "ui_Form.h"
- class ChildAtFunction : public QWidget, public Ui::Form
- {
- Q_OBJECT
- public:
- explicit ChildAtFunction(QWidget *parent = 0);
- public slots:
- void mousePressEvent ( QMouseEvent * event );
- };
- #endif // CHILDATFUNCTION_H
- // childatfunction.cpp
- #include "childatfunction.h"
- ChildAtFunction::ChildAtFunction(QWidget *parent) : QWidget(parent)
- {
- setupUi(this);
- qDebug() << "Address = " << label;
- qDebug() << "Address = " << widget_1;
- qDebug() << "Address = " << widget_2;
- qDebug() << "Address = " << widget_3;
- }
- void ChildAtFunction::mousePressEvent ( QMouseEvent * event)
- {
- qDebug() << "Catch Mouse Press Event: "<< childAt(event->pos());
- }
UI:
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>Form</class>
- <widget class="QWidget" name="Form">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>462</width>
- <height>308</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>Form</string>
- </property>
- <widget class="QWidget" name="widget_1" native="true">
- <property name="geometry">
- <rect>
- <x>160</x>
- <y>10</y>
- <width>120</width>
- <height>80</height>
- </rect>
- </property>
- <property name="palette">
- <palette>
- <active>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </active>
- <inactive>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </inactive>
- <disabled>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </disabled>
- </palette>
- </property>
- <property name="autoFillBackground">
- <bool>true</bool>
- </property>
- </widget>
- <widget class="QWidget" name="widget_2" native="true">
- <property name="geometry">
- <rect>
- <x>150</x>
- <y>130</y>
- <width>120</width>
- <height>80</height>
- </rect>
- </property>
- <property name="palette">
- <palette>
- <active>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </active>
- <inactive>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </inactive>
- <disabled>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </disabled>
- </palette>
- </property>
- <property name="autoFillBackground">
- <bool>true</bool>
- </property>
- </widget>
- <widget class="QWidget" name="widget_3" native="true">
- <property name="geometry">
- <rect>
- <x>300</x>
- <y>80</y>
- <width>120</width>
- <height>80</height>
- </rect>
- </property>
- <property name="palette">
- <palette>
- <active>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </active>
- <inactive>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>255</red>
- <green>255</green>
- <blue>255</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </inactive>
- <disabled>
- <colorrole role="Base">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- <colorrole role="Window">
- <brush brushstyle="SolidPattern">
- <color alpha="255">
- <red>99</red>
- <green>240</green>
- <blue>186</blue>
- </color>
- </brush>
- </colorrole>
- </disabled>
- </palette>
- </property>
- <property name="autoFillBackground">
- <bool>true</bool>
- </property>
- </widget>
- <widget class="QLabel" name="label">
- <property name="geometry">
- <rect>
- <x>30</x>
- <y>90</y>
- <width>71</width>
- <height>71</height>
- </rect>
- </property>
- <property name="text">
- <string>TextLabel</string>
- </property>
- </widget>
- </widget>
- <resources/>
- <connections/>
- </ui>
转载于:https://blog.51cto.com/neicole/970107
QWidget中的ChildAt()函数使用试验相关推荐
- 【Qt】QWidget类详解(函数篇)
00. 目录 文章目录 00. 目录 01. 概述 02. Reimplemented Public Functions 03. Public Slots 04. Signals 05. Static ...
- QT学习-界面中实时绘制函数图像
通过重写QT中QWidget类中的paintEvent函数,我们就可以做到在widget中进行函数图像的绘制.(我使用的是QCreator的UI设计器) 首先我们需要从QWidget继承一个子类,并重 ...
- OpenCV 相机校正过程中,calibrateCamera函数projectPoints函数的重投影误差的分析
OpenCV 校正过程中,calibrateCamera函数的ret和重投影误差的分析 OpenCV对相机进行校正的过程中,校正返回值retval和重投影误差的计算公式表示和分析. OpenCV 校正 ...
- Iar环境c语言调用汇编函数,如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数?...
我在硬故障处理程序中有一些程序集.程序集基本上是为了传递当前堆栈指针作为参数(在R0中).它看起来像这样...如何在IAR EWARM中通过内联汇编程序在另一个模块中调用C函数? __asm(&quo ...
- C++11 :STL中的 iota ()函数
该函数是C++11 才引入,之前版本没有此函数. iota 函数是一个计算机语言中的函数,用于产生连续的值.该函数得名自 APL 语言,其中用来产生从 1 开始的连续数值. 该函数位于头文件#incl ...
- Matlab中的lsqcurvefit函数的使用
Matlab中的lsqcurvefit函数的使用 lsqcurvefit函数 调用示例 lsqcurvefit函数 非线性曲线拟合是已知输入向量xdata和输出向量ydata,并且知道输入与输出的函数 ...
- 在Vue的webpack中结合runder函数
在Vue的webpack中结合runder函数 1.引入: <h1>下面是vue的内容:</h1><div id="app"><login ...
- 【OpenCV】OpenCV中积分图函数与应用
OpenCV中积分图函数与应用 参考资料 opencv 查找integral,目前网上大部分的资料来自于opencv https://docs.opencv.org/master/d7/d1b/gro ...
- Oralce中的to_date()函数
Oralce中的to_date()函数 to_date( '".$params['ORDER_TIME']."','YYYY-MM-DD') TO_DATE(:BEGIN_DA ...
最新文章
- PKI与证书服务应用以及相关安全协议
- Java程序员春招三面蚂蚁金服,1200页文档笔记
- 虚拟机linux中怎样打开qt,虚拟机中在Centos 4.7中安装qt-x11-opensource-4.4.3
- 烟台大学计算机专业调剂贴吧,烟台大学计算机与控制工程学院2021年考研复试与调剂的说明...
- 怎么才能升级成鸿蒙系统,怎么能升级成鸿蒙系统
- python 回溯法 01背包问题_回溯法解决01背包问题
- 对Spring的IoC和DI最生动的解释
- 深入理解redis复制原理
- 机器学习基础算法11-Logistic回归-ROC和AUC分类模型评估-实例
- 【推荐】HTML5 Word Cloud——中文词云
- Nature呼吁:应对21世纪的大规模流行病,各个学科必须联合起来
- wps——ppt中的视频提取
- html点击按钮弹出悬浮窗_html弹窗,html网页弹窗代码
- 旧约圣经对基督(弥赛亚)的预言
- markdown的各种操作
- 在服务器创建并进入虚拟环境
- android 旋转屏幕 不重走生命周期,屏幕旋转后Activity生命周期
- C++ 判断路径是否存在,不存在则创建(包含多级创建)
- 什么叫显示动力学,什么叫隐式动力学分析!
- 微信小程序js把数字转化成字母