QLineEdit *lineEdit = new QLineEdit(); QPushButton *button = new QPushButton(); QHBoxLayout *layout = new QHBoxLayout(); layout->addWidget(lineEdit); layout->addWidget(button); QWidget *container = new QWidget(); container->setLayout(layout); container->show(); QWidget *widgetWithFocus = container->focusWidget(); if (widgetWithFocus) { qDebug() << "The focus widget is of type:" << widgetWithFocus->metaObject()->className(); } else { qDebug() << "No widget has focus."; }
QWidget *container = new QWidget(); QLineEdit *lineEdit = new QLineEdit(container); QPushButton *button = new QPushButton(container); QHBoxLayout *layout = new QHBoxLayout(container); layout->addWidget(lineEdit); layout->addWidget(button); container->show(); // Set focus to the QLineEdit widget lineEdit->setFocus();In this example, we create a container widget with a QLineEdit and QPushButton widget added to it using a QHBoxLayout. Then, we call the setFocus method on the QLineEdit widget to set it as the current focus widget when the container is shown. Package library: Qt