#include#include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget parentWidget; parentWidget.setGeometry(200, 200, 400, 400); QPushButton button("Button", &parentWidget); button.setGeometry(50, 50, 100, 50); QLabel label("Label", &parentWidget); label.setGeometry(150, 150, 100, 50); QWidget *childWidget = parentWidget.childAt(75, 75); if (childWidget) { qDebug() << childWidget->objectName() << " is under the point (75,75)"; } return a.exec(); }
#includeThese examples are part of the Qt library.#include class MyWidget : public QWidget { public: MyWidget() { setFixedSize(400, 400); } protected: void mousePressEvent(QMouseEvent *event) { int x = event->x(); int y = event->y(); QWidget *childWidget = childAt(x, y); if (childWidget) { qDebug() << "Mouse clicked on " << childWidget->objectName(); } } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MyWidget parentWidget; QPushButton button("Button", &parentWidget); button.setObjectName("Button"); button.setGeometry(50, 50, 100, 50); QLabel label("Label", &parentWidget); label.setObjectName("Label"); label.setGeometry(150, 150, 100, 50); parentWidget.show(); return a.exec(); }