Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Key Events not handled by QQuickView before mouse-click [SOLVED]
QtWS25 Last Chance

Key Events not handled by QQuickView before mouse-click [SOLVED]

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qquickwidgetqquickview
3 Posts 2 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frankiefrank
    wrote on last edited by frankiefrank
    #1

    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).

    "Roads? Where we're going, we don't need roads."

    p3c0P 1 Reply Last reply
    0
    • F frankiefrank

      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).

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @frankiefrank Is it the same behavior with QQuickWidget ?

      157

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frankiefrank
        wrote on last edited by
        #3

        Great tip! No, using QQuickWidget seems to solve the problem!

        "Roads? Where we're going, we don't need roads."

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved