如何切换弹出窗口的焦点?

时间:2018-02-21 08:05:52

标签: qt qml

我遇到一个问题,即弹出窗口在显示时无法获得焦点。我试图在主窗口中使用activefocus功能,但它不起作用。假设如果我按下回车键,弹出窗口将被关闭。如何获得弹出窗口的焦点?感谢。

...
GridView {
                   id:grid_main
                   anchors.fill: parent                       
                   focus: true
                   currentIndex: 0

                   model: FileModel{
                       id: myModel
                       folder: "c:\\folder"  
                       nameFilters: ["*.mp4","*.jpg"]
                   }

                   highlight: Rectangle { width: 80; height: 80; color: "lightsteelblue" }

                   delegate: Item {
                       width: 100; height: 100

                       Text {
                           anchors { top: myIcon.bottom; horizontalCenter: parent.horizontalCenter }
                           text: fileName
                       }
                       MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               parent.GridView.view.currentIndex = index    
                           }
                       }
                   }

                   Keys.onPressed: { //pop up window
                            if (event.key == 16777220) {//enter                                    
                                subWindow.show();                                    
                                subWindow.forceActiveFocus();
                                event.accepted = true;
                                grid_main.focus = false;
                            }
                   }
    }

   Window {
           id: subWindow

           Keys.onPressed: {
                    if (event.key == 16777220) {//press enter                           
                        subWindow.close();
                    }
           }
    }
...

1 个答案:

答案 0 :(得分:2)

让我们从一些基础开始:

Keys.onPressed: { //pop up window
    if (event.key == 16777220) {//enter
        subWindow.show()
        ...
        event.accepted = true
    }
}

更不用说它是多么容易出错,只是为了便于阅读,请不要对16777220之类的枚举值进行硬编码。 Qt提供Qt.Key_ReturnQt.Key_Enter(通常位于键盘上)以及更方便的Keys.returnPressedKeys.enterPressed信号处理程序。这些便利处理程序甚至自动设置event.accepted = true,因此您可以用更简单的版本替换信号处理程序:

Keys.onReturnPressed: {
    subWindow.show()
    ...
}

现在,接下来要找到正确的调用方法。首先,QML窗口类型没有forceActiveFocus()这样的方法。如果你注意应用程序输出,你应该看到:

  

TypeError:Property' forceActiveFocus'对象QQuickWindowQmlImpl(0x1a6253d9c50)不是函数

该文档包含可用方法的列表:Window QML type。您可能希望尝试show()requestActivate()的组合。

Keys.onReturnPressed: {
    subWindow.show()
    subWindow.requestActivate()
}

然后,您想要处理子窗口中的键。目前,您正在尝试将QML密钥附加到窗口。同样,如果你注意应用程序输出,你应该看到:

  

无法将Keys属性附加到:QQuickWindowQmlImpl(0x1ddb75d7fe0)不是项目

也许它只是简化的测试用例,但是当你提供测试用例时,你需要把这些东西弄好,以避免人们关注错误的错误。无论如何,你想要做的是创建一个项目,请求焦点,并处理它上面的键:

Window {
    id: subWindow

    Item {
        focus: true
        Keys.onReturnPressed: subWindow.close()
    }
}

最后,为了将各个部分放在一起,一个工作的最小测试用例看起来像:

import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    id: window
    width: 300
    height: 300
    visible: true

    GridView {
        focus: true
        anchors.fill: parent
        // ...
        Keys.onReturnPressed: {
            subWindow.show()
            subWindow.requestActivate()
        }
    }

    Window {
        id: subWindow
        Item {
            focus: true
            anchors.fill: parent
            Keys.onReturnPressed: subWindow.close()
        }
    }
}

PS。关键事件依赖于焦点在您期望的位置。例如,如果用户选项卡将焦点导航到其他位置,则可能并非总是如此。考虑使用Shortcut QML type以更可靠的方式关闭弹出窗口。