Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A bug about Keyboard Focus in Qt Quick in QT 5.10 document?

A bug about Keyboard Focus in Qt Quick in QT 5.10 document?

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 464 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.
  • M Offline
    M Offline
    mrmuto2
    wrote on last edited by
    #1

    I am reading the Qt doc to learn the Keyboard Focus in Qt Quick! I run the code which from the document. However, the result is different form the document! The codes are as follows.

    main.qml

    //Window code that imports MyWidget
    Rectangle {
    id: window
    color: "white"; width: 240; height: 150
    
    Column {
        anchors.centerIn: parent; spacing: 15
    
        MyWidget {
            focus: true             //set this MyWidget to receive the focus
            color: "lightblue"
        }
        MyWidget {
            color: "palegreen"
        }
    }
    

    MyWidget.qml

    Rectangle {
      id: widget
      color: "lightsteelblue"; width: 175; height: 25; radius: 10; antialiasing: 
      true
      Text { id: label; anchors.centerIn: parent}
      focus: true
      Keys.onPressed: {
          if (event.key == Qt.Key_A)
              label.text = 'Key A was pressed'
          else if (event.key == Qt.Key_B)
              label.text = 'Key B was pressed'
          else if (event.key == Qt.Key_C)
              label.text = 'Key C was pressed'
       }
    }
    

    The pic1 is the result form doc. And the pic2 is the result of my running which i just copy the code from doc and run it.

    pic1
    pic1

    pic2
    pic2

    Is it a bug? Why the result are different?

    The doc said:

    We want the first MyWidget object to have the focus, so we set its focus property to true. However, by running the code, we can confirm that the second widget receives the focus.

    Looking at both MyWidget and window code, the problem is evident - there are three types that set the focus property to true. The two MyWidgets set the focus to true and the window component also sets the focus. Ultimately, only one type can have keyboard focus, and the system has to decide which type receives the focus. When the second MyWidget is created, it receives the focus because it is the last type to set its focus property to true.

    My question:

    1.Why the result are different?In my result the first widget receives the focus.

    2.Also, what is the meaning of"because it is the last type to set its focus property to true"in doc?

    The doc you can get from here!
    Qt doc

    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