Please nominate your Qt Champions for 2021!

ActiveFocus not changing after Qml Window re activated

  • Hello,
    running on MacOS i have a Window or ApplicationWindow which gets created from C++.

    I think the problem must be how i create the window from C++ because if i run it in plain qml it works fine...

    QQmlApplicationEngine engine; // is a class member declared in .h
    vod MainWindow::MyWindow()
        QQmlContext *ctxt = engine.rootContext();
        ctxt->setContextProperty("DatabaseManager", &DatabaseManager::instance());
        qmlRegisterType<SomeType>("my.qmlcomponents", 1, 0, "SomeType");
        connect(&m_engine, SIGNAL(objectCreated(QObject *, const QUrl &)),
                this, SLOT(qmlEngineObjectCreated(QObject *, const QUrl &)));
    void MainWindow::qmlEngineObjectCreated(QObject *object, const QUrl &url)
        m_resolverQmlObject = object;  // m_resolverQmlObject is QObject type
        QWidget *w = QWidget::createWindowContainer(qobject_cast<QQuickWindow*>(object), this);
        qDebug() << "Got EditDialog";
        w->setParent(nullptr); // Without this the Window is not coming up
        QQmlEngine::setObjectOwnership(m_resolverQmlObject, QQmlEngine::CppOwnership);

    Inside the Window i have for example some TextField components.
    Everything works fine. If i click inside a TextField it gets the focus and i can enter text.

    And i see in my (root) Window onActiveFocusItemChanged gets triggered each time i click in another TextField.

    Now if i click outside my application and the app looses focus and the click again into the application Window inside a TextField the TextField gets focus as before but no KeyEvents occur. So if i type the TextField does not show the typed characters.
    Also onActiveFocusItemChanged gets not called anymore.

    Any idea why activeFocusItem isn't updating after the Window lost focus and then got focus again?

    Window {
        onActiveFocusItemChanged: {
            print("activeFocusItem: ", activeFocusItem)
         RowLayout {
              anchors.fill: parent
              TextField {
                  id: tf1
                  onFocusChanged: {
                      print("TextField 1 got focus")
              TextField {
                  id: tf2
                  onFocusChanged: {
                      print("TextField 2 got focus")


Log in to reply