Key Events not handled by QQuickView before mouse-click [SOLVED]
-
I have an issue with key events in some basic QML, when loaded in a widget application,.
The key events are not handled after the window is loaded. I have to actually click on the QML scene once and only after that the key events work.
I suspect this is a Qt Bug but maybe anyone can tell me how I can really make the key events work immediately?
Here's my QML code: (please forgive the formatting, with the new forums I can't manage to make simple code tags work for some reason)
// FocusTest.qml import QtQuick 2.4 Rectangle { id: mainContainer width: 700 height: 700 // Some optional debug messages to see if/when focus or activeFocus is changed // onActiveFocusChanged: console.log("active focus of mainContainer changed: " + activeFocus) // onFocusChanged: console.log("focus of mainContainer changed to " + focus) // This should be enough to make the main container in focus focus: true Keys.onPressed: { txt.text = event.key console.log("Keys.onPressed: " + event.key); } Text { id: txt width: 300 height: 300 anchors.centerIn: parent text: "expect key events here..." } }
Now I made a simple Qt Widgets Application, and to the default MainWindow class I added a QQuickView member.
In MainWindow.cpp constructor:
ui->setupUi(this); quickView = new QQuickView(); QWidget *containerWidget = QWidget::createWindowContainer(quickView); setCentralWidget(containerWidget); quickView->setSource(QUrl::fromLocalFile("D:\\FocusSample.qml"));
I tried setting focus to true with a timer, I tried calling forceActiveFocus(), but both didn't work for me. The key events only get handled after the first mouse click.
Is there anything I can do to force the focus? Is there a way to simulate that first click, or another way to force focus?
Also - the same QML works as expected when not loaded as a widget (i.e. if I create a .qmlproject to contain the QML only and run that with Qt Creator).
-
@frankiefrank Is it the same behavior with
QQuickWidget
? -
Great tip! No, using QQuickWidget seems to solve the problem!